Dsa
Monotonic Stack
Easy
Building Height Visibility
You are given the heights of buildings in a row. For each building, determine how many consecutive buildings to the right can be seen without being blocked by taller buildings. A building can see the next building if it is shorter than itself. Your program should read the number of buildings and then their heights, and for each building, print the number of buildings it can see.
Input format:
The first line contains a single integer, n (1 <= n <= 100000), the number of buildings.
The second line contains n integers representing the building heights, where each height is between 1 and 100000.
Output format:
Print n integers as space-separated values, where each integer represents the number of visible buildings.
Example:
Input:
5
5 3 4 2 1
Output:
1 1 1 0 0
Key concepts
monotonic_stackarraybuilding
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