#STK0000084. Phần tử lớn hơn tiếp theo II (Next Greater Element II)
Phần tử lớn hơn tiếp theo II (Next Greater Element II)
Next Greater Element II
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Treat the array as circular: after the last element comes the first element again. For every position, find the first greater value encountered when moving right around the circle. The element itself cannot be used as its own answer. If no greater value exists, print .
Input
The first line contains . The second line contains the array values.
Output
Print integers, the circular next greater value for each position, or .
Subtasks
- Subtask 1 (30 points): ; all other conditions are unchanged.
- Subtask 2 (70 points): , .
Examples
Input
3
1 2 1
Output
2 -1 2
Explanation
The circular array is .
- For the at position , moving right immediately reaches , so the answer is .
- For the at position , moving right visits the at position and then wraps to the at position ; neither is greater than , so the answer is .
- For the at position , wrapping around first reaches the equal value at position , then reaches at position , so the answer is .
Therefore the output is 2 -1 2.