Make Array Zero by Subtracting Equal Amounts (LeetCode 2357): Solution and Explanation

Easy
Array
Hash table
Greedy
Sorting
Heap (priority queue)
Simulation

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

  1. Push every positive value into a min-heap.
  2. Pop values in increasing order, tracking removed, the current height of the cut line.
  3. When a popped value is above removed, that is one operation: add 1 and set removed to it.
  4. Values equal to removed are already zero and cost nothing. Return the count.

Step-by-step example

nums = [1,5,0,3,5]
  1. Heap of positives: 1, 3, 5, 5. The zero is ignored.
  2. Pop 1: above the line at 0, so operation 1 subtracts 1. The line is at 1.
  3. Pop 3: operation 2 subtracts 2. The line is at 3.
  4. Pop 5: operation 3 subtracts 2 and both 5s reach zero. The second 5 is popped for free.
  5. 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 ops
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

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).

Space

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.