LeetCode Challenge Day 56 β 3542. Minimum Operations to Convert All Elements to Zero
Nitin Ahirwal / November 10, 2025
Hey folks π
This is Day 56 of my LeetCode streak π
Todayβs problem is 3542. Minimum Operations to Convert All Elements to Zero β a medium yet elegant problem that blends monotonic stack and greedy logic to find the minimum number of operations to reduce all elements of an array to zero.
π Problem Statement
You are given an array nums consisting of non-negative integers.
In one operation:
- Select a subarray
[i, j]. - Find the minimum non-zero value within that subarray.
- Set all occurrences of that value in the subarray to
0.
Return the minimum number of operations required to make all elements in the array equal to 0.
Example 1:
Input: nums = [0,2]
Output: 1
Explanation:
Select subarray [1,1] β min = 2 β set 2 β 0 β [0,0]
Example 2:
Input: nums = [3,1,2,1]
Output: 3
Explanation:
Step 1: Subarray [1,3] β min = 1 β [3,0,2,0]
Step 2: Subarray [2,2] β min = 2 β [3,0,0,0]
Step 3: Subarray [0,0] β min = 3 β [0,0,0,0]
π‘ Intuition
We need to minimize the number of operations.
A brute-force approach would repeatedly find subarrays and zeros, but thatβs too costly.
Instead, think in terms of value progression:
- Each distinct positive increase in the array requires one new operation.
- Zeros act as boundaries that separate different operation groups.
- Whenever we see a smaller number, it means previous larger values are already handled β so we βpopβ them out.
This gives us a clear hint to use a monotonic increasing stack.
π Approach
- Initialize an empty stack and an
opscounter. - Traverse through each element
ainnums:- While the stackβs top is greater than
a, pop it β because those values no longer matter. - If
a > 0and (stackis empty or top <a), push it and incrementops. - Ignore zeros completely (they separate operation segments).
- While the stackβs top is greater than
- The final
opscount gives the minimum operations needed.
Each time we push a new element, it signifies starting a new subarray operation.
β±οΈ Complexity Analysis
-
Time Complexity:
( O(n) ) β each element is pushed and popped at most once. -
Space Complexity:
( O(n) ) β in the worst case (strictly increasing array).
π§βπ» Code (JavaScript)
/**
* @param {number[]} nums
* @return {number}
*/
var minOperations = function(nums) {
const stack = [];
let ops = 0;
for (let a of nums) {
// Remove values greater than current a
while (stack.length && stack[stack.length - 1] > a) {
stack.pop();
}
// If current is positive and different from stack top, we need a new operation
if (a > 0) {
if (!stack.length || stack[stack.length - 1] < a) {
stack.push(a);
ops++;
}
}
// if a == 0 -> we do not push zeros (treated as separators)
}
return ops;
};
π§© Example Walkthrough
Input:
nums = [1,2,1,2,1,2]
Process:
Step 1 β Push 1 β ops = 1 Step 2 β Push 2 β ops = 2 Step 3 β Pop 2 (since 1 < 2), Push 1 β no new op (already counted) Step 4 β Push 2 β ops = 3 Step 5 β Pop 2 β Push 1 β no new op Step 6 β Push 2 β ops = 4
Output:
4
π― Reflection
This problem beautifully demonstrates how stack-based thinking can simplify complex subarray operations.
Instead of simulating each subarray removal, we just track value transitions β leading to an optimal and clean O(n) solution.
Thatβs it for Day 56 of my LeetCode challenge πͺ
Keep stacking up your logic, one element at a time β‘
Happy Coding π¨βπ»