Data Structures · Visualization

Monotonic Decreasing Stack

Stack

A monotonic decreasing stack keeps its values sorted from bottom (largest) to top (smallest) — the mirror image of the increasing version. Time: O(n) total across all pushes/pops. Space: O(n).

Big-O: push / pop / peek are all O(1).

Monotonic Decreasing Stack

Stack

A monotonic decreasing stack keeps its values sorted from bottom (largest) to top (smallest) — the mirror image of the increasing version. Before pushing a new value, you pop off any values from the top that are less than or equal to the new value, so the decreasing order is preserved. This is the go-to pattern for efficiently finding the nearest greater value to the left or right of each element.

How Monotonic Decreasing Stack Works

  1. Start with an empty stack.
  2. For each new value, while the stack is not empty and the top is less than or equal to the new value, pop it.
  3. Push the new value onto the stack.
  4. Repeat for every element in the input.

Monotonic Decreasing Stack Worked Example

Input: Build a monotonic decreasing stack from [2, 6, 3, 8]

  1. Push 2 → [2]
  2. 6 >= 2, pop 2 → []; push 6 → [6]
  3. 3 <= 6, push 3 → [6, 3]
  4. 8 >= 3, pop 3 → [6]; 8 >= 6, pop 6 → []; push 8 → [8]

Output: [8]

Try your own input
Monotonic Decreasing StackO(n)
Pushes each value, but first pops off anything on top that is smaller — so the stack always reads bottom-to-top in decreasing order.
arr:
3
0
1
1
4
2
2
3
6
4
5
5
bottom
2
6
3
8
← top
Active / highlighted Min stack Result
Monotonic Decreasing Stack
Press ▶ Run to watch it step by step.
Python · Stacks & Queues in Code
def mono_decreasing(arr):
    # maintains a decreasing stack; pops <= current
    stack = [2, 6, 3, 8]
    for x in arr:
        while stack and stack[-1] <= x:
            stack.pop()
        stack.append(x)
    return stack
# example: monodec on [2, 6, 3, 8]
Input: Build a monotonic decreasing stack from [2, 6, 3, 8]

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Monotonic Decreasing Stack Time & Space Complexity

TimeO(n) total across all pushes/pops
SpaceO(n)

When to Use Monotonic Decreasing Stack

  • Finding the 'next greater element' for every item in an array
  • Daily temperatures style problems (how many days until a warmer day)
  • Sliding window maximum style problems

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.