#C8920. Next Greater Element
Next Greater Element
Next Greater Element
Given an array of integers, for each element, find the first element to its right that is greater than the current element. If no such element exists, output -1 for that position.
For example, for the input array [3, 5, 2, 4, 5]
, the output should be [5, -1, 4, 5, -1]
.
Note: The problem requires reading from standard input and writing to standard output.
inputFormat
The first line contains an integer n which denotes the number of elements in the array.
The second line contains n space-separated integers representing the array.
outputFormat
Print the resulting array where each element is replaced by the first greater element to its right. If no such element exists, print -1 for that position. The outputs should be printed as space-separated integers on a single line.
## sample5
3 5 2 4 5
5 -1 4 5 -1