#K78907. Closest Smaller or Equal Element

    ID: 35190 Type: Default 1000ms 256MiB

Closest Smaller or Equal Element

Closest Smaller or Equal Element

You are given an array of N integers. For each element in the array, your task is to find the closest element to its left that is smaller than or equal to it. If there is no such element, output -1 for that position.

Formally, given an array \( A[0\ldots N-1] \), for each index \( i \) (where \( 0 \le i < N \)), find the maximum index \( j \) (where \( 0 \le j < i \)) such that \( A[j] \le A[i] \). If no such \( j \) exists, set the result for that position to \(-1\).

Example:

Input:  5
        1 3 0 2 5
Output: -1 1 -1 0 2

inputFormat

The first line of input contains an integer N denoting the number of elements in the array. The second line contains N space-separated integers representing the array elements.

\( 1 \le N \le 10^5 \) and each element \( A[i] \) is within a reasonable range.

outputFormat

Output a single line containing N space-separated integers where the \( i^{th} \) integer is the closest element to the left of \( A[i] \) that is smaller than or equal to \( A[i] \). If no such element exists, output -1 for that position.

## sample
5
1 3 0 2 5
-1 1 -1 0 2