From e54bc40aca66f8fb0e7a669775c1ca02b8d93d21 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 15 Aug 2024 18:27:01 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1679.=20Max=20Number=20of=20K?= =?UTF-8?q?-Sum=20Pairs=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/max-number-of-k-sum-pairs.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 go/max-number-of-k-sum-pairs.go diff --git a/go/max-number-of-k-sum-pairs.go b/go/max-number-of-k-sum-pairs.go new file mode 100644 index 0000000..15834eb --- /dev/null +++ b/go/max-number-of-k-sum-pairs.go @@ -0,0 +1,24 @@ +package main + +import "slices" + +func maxOperations(nums []int, k int) int { + slices.Sort(nums) + + operations := 0 + + l, r := 0, len(nums)-1 + for l < r { + if nums[l]+nums[r] == k { + operations++ + l++ + r-- + } else if nums[l]+nums[r] < k { + l++ + } else { + r-- + } + } + + return operations +}