Max Number of K-Sum Pairs

You are given an integer array nums and an integer k.

In one operation, you can pick two numbers from the array whose sum equals k and remove them from the array.

Return the maximum number of operations you can perform on the array.

Example 1
Inputnums = [1,2,3,4], k = 5
Output2
The pairs (1, 4) and (2, 3) each sum to 5, so 2 operations can be performed.
Example 2
Inputnums = [3,1,3,4,3], k = 6
Output1
Only one pair of 3's can be removed to sum to 6, so 1 operation can be performed.

Constraints

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

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate