Make Array Zero by Subtracting Equal Amounts (LeetCode 2357): Solution and Explanation
Each operation subtracts the same positive x (no larger than the smallest non-zero element) from every positive element. Return the fewest operations that turn nums into all zeros.
Intuition
Picture each value as a bar. Subtracting x from every positive bar is the same as raising a cut line under all of them by x: anything below the line is gone.
The line can never pass the shortest positive bar in one move, because x is capped by the smallest non-zero value. So each operation can at best stop exactly on that bar's top and zero it.
Bars of equal height reach zero together, and bars of different heights never do. So every distinct positive value costs exactly one operation, and nothing else costs anything.
Popping the positive values from a min-heap in increasing order and counting each new value gives the stops of the cut line.
Approach: Min-heap of positive values
- Push every positive value into a min-heap.
- Pop values in increasing order, tracking
removed, the current height of the cut line. - When a popped value is above
removed, that is one operation: add 1 and setremovedto it. - Values equal to
removedare already zero and cost nothing. Return the count.
Step-by-step example
nums = [1,5,0,3,5]
- Heap of positives: 1, 3, 5, 5. The zero is ignored.
- Pop 1: above the line at 0, so operation 1 subtracts 1. The line is at 1.
- Pop 3: operation 2 subtracts 2. The line is at 3.
- Pop 5: operation 3 subtracts 2 and both 5s reach zero. The second 5 is popped for free.
- The answer is 3.
Code: Python, Java, C++
1class Solution:
2 def minimumOperations(self, nums: List[int]) -> int:
3 heap = [x for x in nums if x > 0]
4 heapq.heapify(heap)
5 ops = 0
6 removed = 0
7 while heap:
8 x = heapq.heappop(heap)
9 if x > removed:
10 ops += 1
11 removed = x
12 return ops1class Solution {
2 public int minimumOperations(int[] nums) {
3 PriorityQueue<Integer> heap = new PriorityQueue<>();
4 for (int x : nums) {
5 if (x > 0) heap.offer(x);
6 }
7 int ops = 0, removed = 0;
8 while (!heap.isEmpty()) {
9 int x = heap.poll();
10 if (x > removed) {
11 ops++;
12 removed = x;
13 }
14 }
15 return ops;
16 }
17}1class Solution {
2public:
3 int minimumOperations(vector<int>& nums) {
4 priority_queue<int, vector<int>, greater<int>> heap;
5 for (int x : nums) {
6 if (x > 0) heap.push(x);
7 }
8 int ops = 0, removed = 0;
9 while (!heap.empty()) {
10 int x = heap.top();
11 heap.pop();
12 if (x > removed) {
13 ops++;
14 removed = x;
15 }
16 }
17 return ops;
18 }
19};Time and space complexity
O(n log n)
Each of the n values is pushed and popped once. A hash set of positive values brings this down to O(n).
O(n)
The heap holds up to n values.
Other approaches
Hash set (time O(n), space O(n))
Return the size of the set of positive values. Shortest code and linear time.
Direct simulation (time O(n^2), space O(1))
Repeatedly find the smallest positive value and subtract it from every positive element until all are zero. Correct for these small limits but O(n^2).
Common mistakes
- Counting 0 as a distinct value, which adds an extra operation.
- Counting every element instead of distinct values, so duplicates cost extra.
- Subtracting from zeros in a simulation, which drives them negative.
Frequently asked questions
Why can one operation never zero two different values?
Two elements with different values keep the same difference after any operation, because both lose the same x. So they cannot both become 0 in the same step.
Why is the smallest non-zero value always the right choice for x?
A smaller x zeroes nothing and wastes the operation. x cannot be larger by the rules. Taking exactly that value zeroes its bars and moves on.
Is the heap needed?
No. A set of positive values gives the same count in O(n). The heap version mirrors the operations one by one, which makes the mechanism easy to follow.