1679. Max Number of K-Sum Pairs
T: O(n)
S: O(n)
```java
class Solution {
public int maxOperations(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
int result = 0;
for (int num : nums) {
if (map.containsKey(k - num)) {
result++;
int count = map.get(k - num) - 1;
if (count == 0) {
map.remove(k - num);
} else {
map.put(k - num, count);
}
} else {
map.put(num, map.getOrDefault(num, 0)+1);
}
}
return result;
}
}
/***
T: O(n)
S: O(n)
how many k sum pair in this array
use hashmap
4
3
3 -> 1
3
1
4 3 2 1
4
3
2 -> 1
1. -> 2
*/
```
Last updated