Dsa
Monotonic Stack
Medium
Minimize Distance
You are given a sequence of integers representing positions on a line. For each position, calculate the distance to the nearest position that is smaller than the current position, going to the right. If none exists, output -1 for that position.
Input format:
- A single line containing space-separated integers, representing the positions.
Output format:
- A single line containing space-separated integers.
Example:
Input:
5 3 1 4 2
Output:
1 1 1 1 -1
Key concepts
monotonic_stackarray
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now