#2357
Easy Algorithms Make array zero by subtracting equal amounts
Array Hash Table Greedy Sorting Heap (Priority Queue) Simulation
73.7% acceptance
Feb 25, 2026
1316
61
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.
Solution
Rust
Time O(n)
Space O(1)
use std::collections::HashSet;
impl Solution {
pub fn minimum_operations(nums: Vec<i32>) -> i32 {
nums.iter().filter(|&&x| x > 0).map(|&x| x).collect::<HashSet<i32>>().len() as i32
}
}