Dsa
Arrays
Easy
Rain Water Collection
Given an array representing the heights of bars in a histogram, calculate how much water can be trapped after raining. The bars can hold water based on the height of the shorter bar to its left and right.
Input Format:
- The first line contains an integer n (1 ≤ n ≤ 100), the size of the array.
- The second line contains n space-separated integers representing the heights of the bars.
Output Format:
- Print the total amount of water that can be trapped.
Example:
Input:
5
0 1 0 2 1
Output:
1
Key concepts
arraysprefix sumswater collection
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