Skip to content

Max Number of K-Sum Pairs

MediumTwo Pointers
Decode it first. Clues, constraints, pattern and plan before any code.

You are given a list of integers nums and an integer k. In one operation you pick two numbers whose sum equals k and remove them from the list. Return the maximum number of operations you can perform. Each element can be used at most once.

nums = [1, 2, 3, 4],    k = 5   →  2   (remove 1 and 4, then 2 and 3)
nums = [3, 1, 3, 4, 3], k = 6   →  1   (remove two 3s; the last 3 has no partner)

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 1 <= k <= 10^9