go: add «1679. Max Number of K-Sum Pairs»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-15 18:27:01 +02:00
parent f57c9959f7
commit e54bc40aca
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -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
}