#K65577. Nearest Greater Element to the Right

    ID: 32228 Type: Default 1000ms 256MiB

Nearest Greater Element to the Right

Nearest Greater Element to the Right

Given an array of n integers, your task is to find the nearest greater element for every element in the array. The nearest greater element for an element ai is defined as the first element to its right which is strictly greater than ai. If no such element exists, output -1 for that position.

Formally, for each index i (where 0 \le i < n), find the smallest index j such that:

[ j > i \quad \text{and} \quad a_j > a_i, ]

If no index j satisfies the condition, output -1 for that position.

Example:

Input:  5
       4 5 2 10 8
Output: 5 10 10 -1 -1

inputFormat

The input is read from standard input (stdin). The first line contains an integer n representing the number of elements in the array. The second line contains n space-separated integers representing the elements of the array.

Input Format:

 n
 a1 a2 a3 ... an

outputFormat

The output should be printed to standard output (stdout) as a single line containing n space-separated integers, which are the nearest greater elements for each element of the input array. If no greater element exists for a position, output -1 for that position.

Output Format:

 res1 res2 res3 ... resn
## sample
5
4 5 2 10 8
5 10 10 -1 -1