Monotonic stacks

What is the next greater value for 1?

Examples

Worked example

Input
[2, 1, 4, 3]
Expected output
4

Hints and solution

Hint 1

Write down the starting state and walk through one example by hand.

Reveal worked answer

The next position after 1 contains 4, and 4 is larger. It answers both 1 and the earlier 2.

Back to the lesson