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
xsuch thatxis less than or equal to the smallest non-zero element innums. - Subtract
xfrom every positive element innums.
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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code