Free beta: 60 days of full access, no card needed.84 seats leftSign up free

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy

All patterns

Monotonic stack

O(n)

Keep the stack's values decreasing from bottom to top. Pop everything the new value beats, and each pop just found its answer.

Updated Oct 29, 2026

How does Monotonic stack work?

The stack holds indices, not values. Each index waits there for its answer.

Monotonic means the values at the stacked indices only move one way. For next greater, they never rise from bottom to top.

Before pushing a new index, pop every index the new value beats. Each pop is an answer.

The comparison decides what you get. Pop while the top is smaller for next greater.

Flip the comparison to pop while the top is bigger. That gives next smaller.

For the previous greater element, pop while the top's value is not bigger. The index left on top is the answer, and an empty stack means none.

Whatever survives on the stack never found an answer. Those slots keep the default.

The inner loop looks quadratic. Each index is pushed once and popped once, so the total is linear.

Sources: Wikipedia: All nearest smaller values

  1. [0]Push index 0. Value 2 sits on the stack.
  2. [0, 1]Value 1 loses to 2. Nothing pops, push index 1.
  3. [2]Value 5 beats 1, then 2. Both pop, both answer 5.
  4. [2, 3]Value 3 loses to 5. Push index 3.
  5. [2, 3]Input ends. Indices 2 and 3 keep -1.
Next greater element with a monotonic stack
nextGreater([2, 7, 3, 1, 5, 4, 8])
nums
2
7
3
1
5
4
8
stack (indices still waiting)
empty
result
-1
-1
-1
-1
-1
-1
-1

For each number, find the first bigger number to its right. The stack holds the indices still waiting for an answer.

Speed race
Next greater value for each of the same 40 readings

Bars: 40 readings that drift downward, where a bigger value is usually far away.

One operation is one comparison. Every frame, each lane does the same number of operations, so the slow one falls behind.

Scan to the right O(n²)
0 operations
Monotonic stack O(n)
0 operations

 

And with more numbers

Operations for n numbers, and roughly how long they take at 100 million simple operations a second. An estimate from the same count, not a measurement.

n = 1,000
Scan to the right375K · 3.8 ms
Monotonic stack1K · under 1 ms
n = 100,000
Scan to the right3.8B · 38 s
Monotonic stack100K · 1 ms
n = 10,000,000
Scan to the right37.5T · 4.3 days
Monotonic stack10M · 100 ms

The Monotonic stack code template

function nextGreater(nums) {
    const result = new Array(nums.length).fill(-1);
    const stack = [];
    for (let i = 0; i < nums.length; i++) {
        while (stack.length && nums[stack[stack.length - 1]] < nums[i]) {
            result[stack.pop()] = nums[i];
        }
        stack.push(i);
    }
    return result;
}

A worked example of Monotonic stack

Daily temperatures

You get a list of daily temperatures. For each day, count the days until a warmer one.

No warmer day means 0. Nested loops run in O(n squared) and time out at 100000 days.

Daily temperatures is next greater element, with a distance instead of a value.

Walk left to right. Pop every day on the stack that is colder than today.

Today is that day's first warmer one. The wait is the gap between the indices.

Days left on the stack never warmed up. They keep their 0.

function dailyTemperatures(temps) {
    const wait = new Array(temps.length).fill(0);
    const stack = []; // indices, warmest at the bottom

    for (let today = 0; today < temps.length; today++) {
        while (
            stack.length &&
            temps[stack[stack.length - 1]] < temps[today]
        ) {
            const colder = stack.pop();
            wait[colder] = today - colder;
        }
        stack.push(today);
    }

    return wait; // days still on the stack keep their 0
}

When should you use Monotonic stack?

Use Monotonic stack when the problem statement says “next greater / next smaller element”, “daily temperatures until it gets warmer” or “largest rectangle in a histogram”. A solution built on it usually runs in O(n).

  • next greater / next smaller element
  • daily temperatures until it gets warmer
  • largest rectangle in a histogram
  • trap rainwater between bars
  • span/streak that ends at the first bigger or smaller value

The signal words of every pattern sit in one table, the algorithm patterns cheat sheet.

What is Monotonic stack confused with?

Monotonic stack is most often confused with Stack, Monotonic deque, Sliding window (variable), Binary heap and Sorting with a comparator.

  • Stack: A plain stack pops when you say so. A monotonic stack pops on a comparison.
  • Monotonic deque: A deque also drops elements that fall out of a window. With no expiry by age, a stack is enough.
  • Sliding window (variable): A window answers questions about a range. This stack answers about one boundary element per index.
  • Binary heap: A heap gives the global maximum in O(log n). The stack gives the nearest bigger neighbour in O(1).
  • Sorting with a comparator: Sorting throws away positions. Next greater questions are entirely about position.

Common mistakes with Monotonic stack

  • Pushing values instead of indices

    You need the position to compute a distance. Push the index, read the value with temps[i].

  • Getting the comparison backwards

    Smaller on top gives next greater. Bigger on top gives next smaller.

  • Forgetting the leftovers

    Elements still on the stack found no answer. Decide their value up front: -1, 0, or the length.

  • Mishandling equal values

    Strict < keeps duplicates on the stack, <= pops them. Test [2, 2, 2] before you trust it.

Interview problems solved with Monotonic stack include Next greater element, Daily temperatures, Largest rectangle in a histogram, Trapping rain water, Stock span, Remove k digits and Sum of subarray minimums.

  • Next greater element: The pattern with nothing hidden. Everything else is a rewording.
  • Daily temperatures: Next greater, but it asks for the distance.
  • Largest rectangle in a histogram: Each bar needs its first shorter neighbour on both sides.
  • Trapping rain water: Water sits between a bar and the next taller one.
  • Stock span: Next greater run backwards, counting days instead of values.
  • Remove k digits: Pop bigger digits to leave the smallest number.
  • Sum of subarray minimums: Each value owns a span bounded by smaller neighbours.

What is the time and space complexity of Monotonic stack?

The target complexity of Monotonic stack is O(n).

n up to 1e5 elements gives O(n). Each element is pushed once and popped at most once.

See where this fits in the 150-step track