Introduction
The Trapping Rain Water problem involves calculating how much water can be trapped between bars after rainfall.
Given:
- array of heights
Each bar:
- width = 1
The task is to:
- calculate total trapped water
Water trapped at index depends on:
minimum(leftMax,rightMax) - currentHeightThis problem is one of the most important applications of:
Two Pointers + Monotonic Concepts This problem helps in understanding:
- prefix maximums
- suffix maximums
- two pointer optimization
- area calculations
Example
Input:height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output:
6
Explanation:
Total trapped water:
6 units
Constraints
1 <= height.length <= 2 * 10^40 <= height[i] <= 10^5Approach 1 : Brute Force Traversal
Explanation
The simplest way is:
- For every bar:
- find tallest left bar
- find tallest right bar
- Water trapped:
min(leftMax, rightMax) - height[i]Steps
- Traverse every index.
- Find maximum left height.
- Find maximum right height.
- Calculate trapped water.
- Add total water.
Dry Run
Input:[4,2,0,3,2,5]
Index 2:
Left Max = 4 Right Max = 5
Water: min(4,5) - 0 = 4
Total trapped water:
9
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)Explanation:
Left and right maximums are repeatedly searched.
Space Complexity: O(1) Explanation:
No extra arrays are used.
Approach 2 : Two Pointer Optimization
Explanation
The optimized solution uses:
Two PointersIdea:
- maintain:
- left pointer
- right pointer
- track:
- leftMax
- rightMax
Key Observation:
- smaller side determines water level
Process:
- Compare leftMax and rightMax
- Move smaller side inward
- Calculate trapped water
This avoids repeated traversal.
Steps
- Initialize two pointers.
- Maintain leftMax and rightMax.
- Move smaller side.
- Calculate trapped water.
- Return total water.
Dry Run
Input:[4,2,0,3,2,5]
Left = 0
Right = 5
leftMax = 4
rightMax = 5
Move left: Water at index 1:
4 - 2 = 2
Water at index 2: 4 - 0 = 4
Continue...
Total Water:
9
Two Pointer Code
Complexity Analysis
Time Complexity: O(n)Explanation:
Each index is processed once.
Space Complexity: O(1)
Explanation:
Only constant variables are used.
Edge Cases
- Empty array
- Strictly increasing heights
- Strictly decreasing heights
- Flat surface
- Single bar
Why This Problem is Important
Trapping Rain Water helps in understanding:
- Two pointer optimization
- Prefix/suffix maximum concepts
- Area calculations
- Efficient traversal
- Water trapping logic
It is one of the most important advanced array interview problems.
Real-World Applications
Water trapping concepts are used in:
- Terrain analysis
- Flood prediction systems
- Graphics simulations
- Construction planning
- Elevation mapping
Common Mistakes
- Incorrect pointer movement
- Wrong water calculation
- Forgetting max updates
- Using larger side incorrectly
Interview Tips
Interviewers often expect:
- Two pointer explanation
- Water trapping reasoning
- Prefix/suffix logic understanding
Always explain:
- why smaller side determines water
- how leftMax/rightMax work
- why traversal becomes linear
Related Questions
- Largest Rectangle in Histogram
- Container With Most Water
- Daily Temperatures
- Next Greater Element
- Sum of Subarray Minimums
Final Takeaway
The Trapping Rain Water problem is a fundamental advanced array and monotonic concept problem that teaches efficient water calculation and two-pointer optimization techniques. Understanding this problem builds a strong foundation for advanced stack, array, and geometry-based interview problems.