Minimum Number of Operations to Make Array Empty (LeetCode 2870): Solution and Explanation
Each operation deletes two or three equal elements. Return the fewest operations that empty the array, or -1 if some element can never be removed.
Intuition
An operation only ever touches equal values, so the array splits into independent piles, one per distinct value. The answer is the sum over piles, and the whole thing fails if any single pile fails.
A pile of size 1 can never be removed, because every operation needs at least two equal elements. That is the only impossible case: every count of 2 or more is a sum of 2s and 3s.
To use as few operations as possible, remove 3 at a time whenever you can. Look at the remainder of c divided by 3. Remainder 0: all triples. Remainder 2: triples plus one pair. Remainder 1: you cannot leave a single element behind, so give up one triple and turn those 4 elements into two pairs.
All three cases come out to ceil(c / 3) operations, which is (c + 2) // 3 in integer arithmetic.
Approach: Frequency count plus greedy
- Count how many times each value appears.
- For each count c, if c == 1 return -1.
- Otherwise add (c + 2) // 3 to the total.
- Return the total.
Step-by-step example
nums = [2,3,3,2,2,4,2,3,4]
- Counts: 2 appears 4 times, 3 appears 3 times, 4 appears 2 times.
- Value 2: 4 = 2 + 2, so ceil(4 / 3) = 2 operations. Total 2.
- Value 3: one triple, 1 operation. Total 3.
- Value 4: one pair, 1 operation. Total 4.
- No count was 1, so the answer is 4.
Code: Python, Java, C++
1class Solution:
2 def minOperations(self, nums: List[int]) -> int:
3 count = Counter(nums)
4 ops = 0
5 for c in count.values():
6 if c == 1:
7 return -1
8 ops += (c + 2) // 3
9 return ops1class Solution {
2 public int minOperations(int[] nums) {
3 Map<Integer, Integer> count = new HashMap<>();
4 for (int x : nums) count.merge(x, 1, Integer::sum);
5 int ops = 0;
6 for (int c : count.values()) {
7 if (c == 1) {
8 return -1;
9 }
10 ops += (c + 2) / 3;
11 }
12 return ops;
13 }
14}1class Solution {
2public:
3 int minOperations(vector<int>& nums) {
4 unordered_map<int, int> count;
5 for (int x : nums) count[x]++;
6 int ops = 0;
7 for (auto& [value, c] : count) {
8 if (c == 1) {
9 return -1;
10 }
11 ops += (c + 2) / 3;
12 }
13 return ops;
14 }
15};Time and space complexity
O(n)
One pass to count and one pass over the distinct values.
O(n)
The hash map holds one entry per distinct value, up to n entries.
Other approaches
Sort and count runs (time O(n log n), space O(1) extra)
Sort the array and measure each run of equal values instead of using a hash map. It needs no extra map but the sort costs more time.
DP on each count (time O(n + max count), space O(max count))
Compute the fewest 2s and 3s summing to c with a small DP. It works but is unnecessary once you see the ceil(c / 3) pattern.
Common mistakes
- Using c // 3 + c % 3 / 2 style formulas that break when the remainder is 1. A count of 4 needs 2 operations, not an impossible leftover.
- Forgetting that a count of 1 makes the whole answer -1, even if every other value is fine.
- Treating the remainder 1 case as impossible. Only c == 1 is impossible; 4, 7, 10 and so on all work with two pairs at the end.
Frequently asked questions
Why is ceil(c / 3) always enough?
Remainder 0 uses c / 3 triples. Remainder 2 adds one pair. Remainder 1 swaps one triple for two pairs, which costs one extra operation. All three equal ceil(c / 3).
Why can it never do better than ceil(c / 3)?
Each operation removes at most 3 elements, so removing c elements needs at least c / 3 operations, rounded up.
Does the order of values matter?
No. Values never interact, so the total is just the sum of the per-value counts, in any order.