Dsa
Prefix Sum
Warm-up
Water Collected after Rain
You are given an array where each element represents the height of the terrain at that index. Calculate the total amount of water that can be trapped between the terrain after it rains. The input consists of one line, which contains space-separated integers representing the heights. The output should be a single integer representing the total water trapped.
Example Input:
5
0 1 0 2 1 0 1 3 2 1 2 1
Example Output:
6
Key concepts
prefix_sumarrayswater_trapping
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