VISUALGO//PROBLEMS
max number of k-sum pairs · two pointersmedium
sorted nums (target: 5)
1
2
3
4
pairs (0)
·
ELEMENTS
nums
k
Python
def maxOperations(nums: list[int], k: int) -> int:
nums.sort()
left, right = 0, len(nums) - 1
count = 0
while left < right:
total = nums[left] + nums[right]
if total == k:
count += 1
left += 1
right -= 1
elif total < k:
left += 1
else:
right -= 1
return count