RK DSA Studio

Next greater element

For each value, find the first strictly greater value to its right using a stack of unresolved indices.

Remember: A greater arrival resolves smaller waiting values.

Example: [2,1,4] → [4,4,∅].

Time: O(n). Auxiliary space: O(n).

Formula: resolve while a[stack.top] < a[i]

The unresolved stack stores indices in non-increasing value order. ∅ means no greater value.

Complexities describe the underlying algorithm; tutorial snapshots and rendering can use additional resources.

The interactive workspace loads here when JavaScript is available. You can still browse the learning notes and links above without it.