Skip to content

Next Greater Element With a Stack
Step by Step

Next greater element keeps a stack of numbers that still wait for an answer. Every new number pops the smaller ones and becomes their answer.

Step through every push and pop in both directions, then share a link to the step where the stack surprised you.

What You Watch While the Stack Grows and Pops

The stack, the item on top and the answers change on every step.

Numbers wait on the stack until a bigger number arrives. Values fall from bottom to top, so the top always holds the smallest waiting number.

CodeStory's visualiser shows the stack growing and popping with the item on top highlighted. Marked code lines follow every move, and you can step back, step forward or play.

Open Rising first. Then load One big pop to see 6 answer three waiting numbers at once.

How Next Greater Element Pops Waiting Numbers

Steps below follow the stack on your screen.

  1. 1Move through the input from left to right.
  2. 2Pop every waiting number that is smaller than the current one, and record the current number as its answer.
  3. 3Push the current number so it can wait for its own answer.
  4. 4Numbers left on the stack at the end have no greater element.

Next Greater Element Mistakes the Visualiser Shows

A switch in the visualiser turns the mistake on, so you can watch the state go wrong.

  • Brute forceScans everything to the right of every number, up to n(n−1)/2 comparisons. Load Falling to see it.

Next Greater Element Inputs Worth Trying

Four presets cover rising, falling and equal numbers plus one big pop.

  • RisingEvery number is answered by the very next one, so one pop follows one push.
  • FallingNothing ever pops. Switch on brute force here to see it compare every pair.
  • All equalEqual is not bigger, so nothing pops and every answer is −1.
  • One big pop6 answers three waiting numbers at once.

Next Greater Element Cost in Time and Space

Every number is pushed once and popped once, so the work stays linear.

Time
O(n)counted from the pushes and pops you step through
Space
O(n)the stack that holds waiting numbers
Pattern
StackThe latest unfinished number is the one you need.

Practise Stacks Beyond Next Greater Element

Watching shows the stack and solving trains you to spot a waiting list.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Next Greater Element Visualiser FAQs

Open the visualiser and follow the stack push by push.

Open the visualiser
What does the next greater element visualiser show?

CodeStory's visualiser shows the stack growing and popping with the item on top highlighted, beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of next greater element?

O(n) time and O(n) space. Live counts show one push and one pop per number instead of a scan to the right.

What does the stack hold in next greater element?

Numbers that still wait for a greater number sit on the stack, and the smallest one stays on top.

Can I use my own input?

Yes. Enter your own input and the visualiser plays the algorithm on it, including the edge cases that break a solution in an interview.

Is the visualiser free?

Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.

Stuck on Stacks? Get a Free Callback

Tell CodeStory which stack step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from stack basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.