# Leetcode 5618. Maximum number of K sum pairs (hash)

2020-12-06 15:49:50

## 1. subject

Give you an array of integers nums And an integer k .

In every step of the operation , You need to pick from the array And for k Two integers of , And move them out of the array .

Returns what you can execute on an array Maximum operands .

`````` Example  1：
Input ：nums = [1,2,3,4], k = 5
Output ：2
explain ： At the beginning of the  nums = [1,2,3,4]：
-  Removed from the  1  and  4 , after  nums = [2,3]
-  Removed from the  2  and  3 , after  nums = []
No more and for  5  The number of , So at most  2  operations .

Example  2：
Input ：nums = [3,1,3,4,3], k = 6
Output ：1
explain ： At the beginning of the  nums = [3,1,3,4,3]：
-  Remove the first two  3 , after nums = [1,4,3]
No more and for  6  The number of , So at most  1  operations .

Tips ：
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= 10^9
``````

source ： Power button （LeetCode）

## 2. Problem solving

``````class Solution {

public:
int maxOperations(vector<int>& nums, int k) {

unordered_map<int, int> m;
for(auto n : nums)
m[n]++;// Hash count
int sum = 0, num, count, target, add;
for(auto& num_count : m)
{

num = num_count.first;// Numbers
count = num_count.second;// Number
if(count == 0)
continue;
target = k-num;// Another number
if(m.find(target) != m.end())// Another number exists
{

if(target != num)// It's not equal
else if(target == num)// equal
num_count.second -= add;// Original count   Subtract the manipulated
}
}
return sum;
}
};
``````

352 ms 66.5 MB C++

Long click or sweep code pay attention to my official account （Michael amin ）, Come on together 、 Learn together ！

https://chowdera.com/2020/12/20201206154645133w.html