#K48537. Next Greater Element

    ID: 28442 Type: Default 1000ms 256MiB

Next Greater Element

Next Greater Element

Given an array of integers of length \(N\), for each element, find the next greater element to its right. If there is no greater element, output -1.

For instance, for the array [4, 5, 2, 10, 8], the next greater elements array is [5, 10, 10, -1, -1].

The problem requires you to use efficient techniques since a naive approach could lead to higher time complexity.

inputFormat

The input consists of two lines:

  • The first line contains an integer \(N\) denoting the number of elements in the array.
  • The second line contains \(N\) space-separated integers representing the array.

outputFormat

Output a single line with \(N\) space-separated integers. The \(i\)-th integer represents the next greater element for the \(i\)-th element of the array. If there is no such element, print -1 in its place.

## sample
5
4 5 2 10 8
5 10 10 -1 -1

</p>