Problem Summary
You are given an array arr[] where each element represents the height of a building.
Sunlight comes from the left side.
A building can see sunlight if:
There is no taller building before it (to its left)
Important:
Buildings with equal height DO NOT block each other
Key Insight
A building gets sunlight if its height is:
greater than or equal to the maximum height seen so far from the left
Concept (Greedy Approach)
We traverse the array from left to right and track:
maxHeight→ tallest building seen so farcount→ number of buildings receiving sunlight
Rule:
If:
current height >= maxHeight
It gets sunlight.
Then:
Increase
countUpdate
maxHeight
Example Walkthrough
Input
arr = [6, 2, 8, 4, 11, 13]
Step-by-step
| Index | Height | maxHeight | Gets Sunlight? | Count |
|---|---|---|---|---|
| 0 | 6 | 6 | Yes | 1 |
| 1 | 2 | 6 | No | 1 |
| 2 | 8 | 8 | Yes | 2 |
| 3 | 4 | 8 | No | 2 |
| 4 | 11 | 11 | Yes | 3 |
| 5 | 13 | 13 | Yes | 4 |

Join the conversation! Your thoughts help the community grow.