~/algor $ cat problems/stack-keyingi-katta-element
Next greater element
Medium
1000 ms
128 MB
60%
$n$ positive integers $a_{1}, a_{2}, \dots, a_{n}$ are given. For each $a_{i}$ find the first element to its right that is strictly greater than it. If there is no such element, the answer is $-1$.
Input
The first line contains a positive integer $n$. The second line contains $n$ positive integers $a_{i}$ separated by spaces.
Output
One line with $n$ integers separated by spaces — the answer for each element.
Note
$1 \le n \le 2 * 10^{5}$
$1 \le a_{i} \le 10^{9}$
Examples
Input #1
6 4 5 2 10 8 1
Output #1
5 10 10 -1 -1 -1
Input #2
3 3 3 3
Output #2
-1 -1 -1