-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxOperationsOnArray.java
More file actions
33 lines (25 loc) · 903 Bytes
/
Copy pathMaxOperationsOnArray.java
File metadata and controls
33 lines (25 loc) · 903 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
33
package LeetcodePractice;
import java.util.HashMap;
public class MaxOperationsOnArray {
public int maxOperations(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
int operations = 0;
for (int num : nums) {
int complement = k - num;
if (map.getOrDefault(complement, 0) > 0) {
operations++;
map.put(complement, map.get(complement) - 1);
} else {
map.put(num, map.getOrDefault(num, 0) + 1);
}
}
return operations;
}
public static void main(String[] args) {
MaxOperationsOnArray solver = new MaxOperationsOnArray();
int[] nums1 = {1, 2, 3, 4};
System.out.println(solver.maxOperations(nums1, 5));
int[] nums2 = {3, 1, 3, 4, 3};
System.out.println(solver.maxOperations(nums2, 6));
}
}