Max Number of K-Sum Pairs LeetCode solution in C++
1class Solution {2public:3int maxOperations(vector<int> &nums, int k) {4unordered_map<int, int> Map;5for (auto &num : nums) Map[num]++; // count freq of nums6int ans = 0;78for (auto it = Map.begin(); it != Map.end(); ++it) {9int num = it->first, count = it->second;10if (k - num == num)11ans += count / 2; // if num is half of k add half of it's count in ans12else if (Map.count(k - num)) { // find k-num in nums and add min freq of13// num or k-num to ans14int Min = min(count, Map[k - num]);15ans += Min;16Map[num] -= Min;17Map[k - num] -= Min;18}19}2021return ans;22}23};