Array (green = next-greater found)
2
0
1
1
5
2
6
3
3
4
8
5
4
6
Monotonic stack (decreasing, top →)
Stack is empty — push a value
AlgoPlus//structures / monotonic-stack
Read the theory

Monotonic Stack · Next Greater Element

A stack kept in decreasing order; each pop just found its next greater value.

Stability
In-Place
Space Complexity
Avg Time
Legend
Current element
Popped (resolved)
Has next-greater
AI Tutor Workspace
In a nutshell
A monotonic stack answers 'what's the next bigger value to the right?' for every element in one pass. It holds elements still waiting for an answer, kept in decreasing order. When a new value arrives, it pops everything smaller — and each thing popped has just found its next greater element (the new value). Then the new value is pushed to wait its turn. Each element is pushed and popped once, so it's linear time.
Ready
Press play to begin the cinematic walkthrough.
Keep a stack that stays sorted. Before pushing, pop everything it beats — those popped elements just found their 'next greater' answer.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Monotonic Stack · Next Greater Element.