-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmptyArray.java
More file actions
37 lines (29 loc) · 1.33 KB
/
EmptyArray.java
File metadata and controls
37 lines (29 loc) · 1.33 KB
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
34
35
36
import java.util.HashMap;
import java.util.Map;
class Solution {
public int minOperations(int[] nums) {
// Create a HashMap to store the frequency of each element in 'nums'
Map<Integer, Integer> mp = new HashMap<>();
// Count the frequency of each element in 'nums'
for (int num : nums) {
// Put the element in the map and increment its count by 1
mp.put(num, mp.getOrDefault(num, 0) + 1);
}
// Initialize a variable to count the minimum operations needed
int count = 0;
// Iterate through the entries in the HashMap
for (Map.Entry<Integer, Integer> entry : mp.entrySet()) {
// Get the frequency count of the current element
int t = entry.getValue();
// If there is only one occurrence of any element, it's not possible to make it divisible by 3
if (t == 1)
return -1;
// Calculate the number of operations needed to make the frequency divisible by 3
count += t / 3; // Count the complete sets of 3 elements
if (t % 3 != 0) // If there are remaining elements, increment the count
count++;
}
// Return the total count of operations needed
return count;
}
}