Back to posts

LeetCode Challenge Day 56 β€” 3542. Minimum Operations to Convert All Elements to Zero

Nitin Ahirwal / November 10, 2025

LeetCode ChallengeDay 56StackGreedyArrayJavaScriptAlgorithmMedium

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

  1. Initialize an empty stack and an ops counter.
  2. Traverse through each element a in nums:
    • While the stack’s top is greater than a, pop it β€” because those values no longer matter.
    • If a > 0 and (stack is empty or top < a), push it and increment ops.
    • Ignore zeros completely (they separate operation segments).
  3. The final ops count 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 πŸ‘¨β€πŸ’»