Problems > Nearest Smaller to the Left

Nearest Smaller to the Left

Difficulty: 1500 3000 ms 128 MB

stack

For each position, print the position (1-based) of the NEAREST element to its left that is strictly smaller, or 0 if none exists.

Input: the first line has N; the second line has N integers.

Output: N integers: the nearest-smaller positions.

Constraints:

  • 1 ≤ N ≤ 105
  • |ai| ≤ 109

Sample Tests

Input 1
8
2 5 1 4 8 3 2 5
Output 1
0 1 0 3 4 3 3 7
Input 2
1
7
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.