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.

&nbsp;
Example 1:

Input: nums = [1,2,3,4], k = 5
Output: 2
Explanation: Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.

Example 2:

Input: nums = [3,1,3,4,3], k = 6
Output: 1
Explanation: Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.

&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= 109
	1 &lt;= k &lt;= 109


class Solution:
    def maxOperations(self, nums: List[int], k: int) -> int:
        nums.sort()
        left = 0
        right = len(nums) - 1
        ans = 0
        while left < right:
            cur = nums[left] + nums[right]
            if cur == k:
                ans += 1
                left += 1
                right -= 1
            elif cur < k:
                left += 1
            else:
                right -= 1
        
        return ans
class Solution {
    public int maxOperations(int[] nums, int k) {
        HashMap<Integer,Integer>map=new HashMap<>();
        int count=0;
        for(int i=0;i<nums.length;i++){
            //to check if that k-nums[i] present and had some value left or already paired
            if(map.containsKey(k-nums[i])&&map.get(k-nums[i])>0){
                count++;
                map.put(k-nums[i],map.get(k-nums[i])-1);
            }else{
                //getOrDefault is easy way it directly checks if value is 0 returns 0 where I added 1
                //and if some value is present then it return that value "similar to map.get(i)" and I added 1 on it 
                map.put(nums[i],map.getOrDefault(nums[i],0)+1);
            }
        }
        return count;
    }
}
class Solution {
public:
    int maxOperations(vector<int>& nums, int k) {
      unordered_map<int, int> Map;
      for (auto &num: nums) Map[num]++;  // count freq of nums
      int ans = 0;
  
      for(auto it=Map.begin(); it!=Map.end(); ++it){
        int num = it->first, count = it->second;
        if(k - num == num) ans += count/2;   // if num is half of k add half of it's count in ans
        else if(Map.count(k - num)){   // find k-num in nums and add min freq of num or k-num to ans
          int Min = min(count, Map[k-num]);
          ans += Min;
          Map[num] -= Min;
          Map[k-num] -= Min;
        }
      }
      
      return ans;
    }
};
var maxOperations = function(nums, k) {
let freq = new Map(),count=0; 
for (let i = 0; i < nums.length; i++) {
    if (freq.get(k-nums[i])) {
        if(freq.get(k-nums[i])==1) freq.delete(k-nums[i])
        else freq.set(k-nums[i],freq.get(k-nums[i])-1)
        count++;
    }else freq.set(nums[i],freq.get(nums[i])+1||1)
} 
return count;
};
Max Number of K-Sum Pairs