Dsa
Monotonic Stack
Medium
Distance to Nearest Smaller Tower
You are given the heights of several towers positioned in a line. For each tower, calculate the distance to the nearest tower that is shorter. If there's no smaller tower to the left or right, represent this distance as -1.
Input format:
- A single line containing space-separated integers representing the heights of the towers.
Output format:
- A single line containing space-separated integers representing the distances to the nearest smaller tower for each tower.
Example:
Input:
3 4 5 2 1 6
Output:
-1 -1 -1 1 1 -1
Key concepts
monotonic_stackheightsdistance
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