155. Next Smaller Element
Core1000 ms256 MBSolved by 0%
For each element, print the first value to its RIGHT that is strictly smaller
than it, or -1 if there is none.
Print all n answers on one line, separated by single spaces.
This is the previous problem with the comparison flipped — which is exactly the point.
Constraints - `1 ≤ n ≤ 200000` - `-1000000000 ≤ a[i] ≤ 1000000000`
Input
The first line contains an integer n.
The second line contains n space-separated integers.
Output
Print n integers on ONE line, separated by single spaces — each element's answer, or -1.
Input4
4 8 5 2
Output2 5 2 -1
Notehas answers at varying distances
Input4
1 2 3 4
Output-1 -1 -1 -1
Noteis ascending, so nothing has a smaller element to its right
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution