mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
24 lines
296 B
Go
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
|
|
}
|