Make Array Zero by Subtracting Equal Amounts - Problem

You are given a non-negative integer array nums. In one operation, you must:

  • Choose a positive integer x such that x is less than or equal to the smallest non-zero element in nums.
  • Subtract x from every positive element in nums.

Return the minimum number of operations to make every element in nums equal to 0.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,5,0,3,5]
Output: 3
💡 Note: Operation 1: x=1, subtract from all positives → [0,4,0,2,4]. Operation 2: x=2, subtract from all positives → [0,2,0,0,2]. Operation 3: x=2, subtract from all positives → [0,0,0,0,0]. Total: 3 operations.
Example 2 — With Zeros
$ Input: nums = [0]
Output: 0
💡 Note: Array already contains all zeros, so no operations needed.
Example 3 — All Same Values
$ Input: nums = [2,2,2,2]
Output: 1
💡 Note: All elements are the same non-zero value, so only one operation needed to make all zeros.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 0 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Make Array Zero: Key InsightInput: [1,5,0,3,5]15035Unique non-zero values:135Each unique value requires 1 operationAnswer: 3 operations
Understanding the Visualization
1
Input Array
Array with positive and zero values
2
Find Unique
Count distinct non-zero values
3
Operations
Each unique value needs one operation
Key Takeaway
🎯 Key Insight: Count unique non-zero values - each requires exactly one operation to eliminate
Asked in
Google 15 Meta 12
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen