#C3308. Next Greater Element

    ID: 46721 Type: Default 1000ms 256MiB

Next Greater Element

Next Greater Element

Given a list of integers, for each element find the next greater element to its right. If no such element exists, output -1 for that element.

The problem can be solved efficiently using a stack data structure, resulting in a time complexity of \(O(n)\). For each number in the list, you need to find the next number that is greater than it. If there is none, print -1.

For example, for the input [2, 5, 3, 8], the correct output is [5, 8, 8, -1].

inputFormat

The input is given in a single line containing space-separated integers representing the list. If the list is empty, the input will be an empty line.

outputFormat

Output a single line containing the next greater element for each integer from the input, separated by a space. For positions where no greater element exists, output -1.

## sample
2 5 3 8
5 8 8 -1