Problems > Next Greater to the Right

Next Greater to the Right

Difficulty: 1400 3500 ms 128 MB

stack

For every element print the first strictly greater element to its right, or −1 if none exists.

Input: the first line has N; the second line has N integers.

Output: N integers: the next greater element of each position.

Constraints:

  • 1 ≤ N ≤ 105
  • −109 ≤ ai ≤ 109

Sample Tests

Input 1
4
2 1 2 4
Output 1
4 2 4 -1
Input 2
3
3 2 1
Output 2
-1 -1 -1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.