-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMaxOperationsCounter.java
More file actions
32 lines (23 loc) · 937 Bytes
/
MaxOperationsCounter.java
File metadata and controls
32 lines (23 loc) · 937 Bytes
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
26
27
28
29
30
31
32
package two_pointers;
import java.util.HashMap;
public class MaxOperationsCounter {
public int maxOperations(int[] nums, int k) {
HashMap<Integer, Integer> numberCount = new HashMap<>();
int operations = 0;
for (int num : nums) {
if (numberCount.getOrDefault(k - num, 0) > 0) {
operations++;
numberCount.put(k - num, numberCount.get(k - num) - 1);
} else {
numberCount.put(num, numberCount.getOrDefault(num, 0) + 1);
}
}
return operations;
}
public static void main(String[] args) {
MaxOperationsCounter counter = new MaxOperationsCounter();
assert counter.maxOperations(new int[]{1,2,3,4}, 5) == 2 : "Test case 1 failed";
assert counter.maxOperations(new int[]{3,1,3,4,3}, 6) == 1 : "Test case 2 failed";
System.out.println("All test cases passed!");
}
}