1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00
LeetCode/go/max-number-of-k-sum-pairs.go
Matej Focko e54bc40aca
go: add «1679. Max Number of K-Sum Pairs»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-08-15 18:27:01 +02:00

24 lines
296 B
Go

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
}