- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1679.java
25 lines (23 loc) · 742 Bytes
/
_1679.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashMap;
importjava.util.Map;
publicclass_1679 {
publicstaticclassSolution1 {
publicintmaxOperations(int[] nums, intk) {
Map<Integer, Integer> map = newHashMap<>();
intops = 0;
for (intnum : nums) {
if (map.containsKey(k - num)) {
map.put(k - num, map.get(k - num) - 1);
ops++;
if (map.get(k - num) == 0) {
map.remove(k - num);
}
} else {
map.put(num, map.getOrDefault(num, 0) + 1);
}
}
returnops;
}
}
}