Max Number of K-Sum Pairs

Medium

Description

You are given an integer array nums and an integer k. In one operation, pick two numbers from the array whose sum equals k and remove them. Return the maximum number of operations you can perform.

Examples

Input:nums = [1,2,3,4], k = 5
Output:2
Explanation:

Remove (1,4) and (2,3).

Input:nums = [3,1,3,4,3], k = 6
Output:1
Explanation:

It is possible to form one pair that sums to 6: (3,3). After removing one pair of 3's, there are [1,4,3] remaining, but no two numbers sum to 6.

Input:nums = [2,5,4,4,1,3,4,4,1,4,4,1,2,1,2,2,3,2,4,2], k = 5
Output:4
Explanation:

It is possible to form pairs that sum to 5: (1,4), (1,4), (1,4), (2,3). There are four 1's and six 4's, so it is possible to make 4 pairs of (1,4). Also have one 3, so it is possible to make 1 pair of (2,3). Total operations: 4.

Constraints

  • 1 ≤ nums.length ≤ 10^5
  • 1 ≤ nums[i] ≤ 10^9

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!