Skip to main content
Beginnercoding Verified

For each element of an array, find the next greater element to its right (or -1 if none).

ReportedSource: GeeksforGeeks interview experience: Swiggy SDE-2

Quick answerFree

Scan right to left with a decreasing stack: pop elements <= current, the top is the next greater (else -1), then push current. O(n).

Detailed answer· 88 words

Interview Prep

Unlock the full answer with Interview Prep

Get a 88-word model answer and a runnable reference solution, plus 61 more DSA questions.

Also in Pro (₹499 / 90 days).

Reference solution· runs in the browser sandbox

Interview Prep
Unlock with Interview Prep

+ 61 more DSA questions in Interview Prep, each with the full answer.

Discussion