go: add «1679. Max Number of K-Sum Pairs»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
f57c9959f7
commit
e54bc40aca
1 changed files with 24 additions and 0 deletions
24
go/max-number-of-k-sum-pairs.go
Normal file
24
go/max-number-of-k-sum-pairs.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue