A row of n masts is planted left to right, each with a positive integer height. For every mast, look backward (toward masts planted earlier, to its left) and find the nearest earlier mast whose height is strictly smaller than the current mast's height. Report that mast's height, or -1 if no earlier mast is strictly shorter.
Line 1: an integer n.
Line 2: n space-separated integers, the mast heights from left to right.
n space-separated integers on one line: for each mast (left to right), the height of the nearest earlier strictly-shorter mast, or -1.
Example 1
Input
5 6 3 5 2 4
Expected
-1 -1 3 -1 2
Explanation
Mast 6: nothing before it, -1. Mast 3: nothing earlier is shorter, -1. Mast 5: nearest earlier shorter is 3. Mast 2: nothing earlier is shorter, -1. Mast 4: nearest earlier shorter is 2.
Example 2
Input
3 1 2 3
Expected
-1 1 2
Explanation
Heights strictly increase, so each mast's immediate predecessor is shorter: -1, then 1, then 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →