02. Trapping Rain Water
β GFG solution to Trapping Rain Water: calculate maximum water trapped between blocks using efficient two-pointer technique. π
π§© Problem Description
π Examples
Example 1
Input: arr[] = [3, 0, 1, 0, 4, 0, 2]
Output: 10
Explanation: Total water trapped = 0 + 3 + 2 + 3 + 0 + 2 + 0 = 10 units.Example 2
Input: arr[] = [3, 0, 2, 0, 4]
Output: 7
Explanation: Total water trapped = 0 + 3 + 1 + 3 + 0 = 7 units.Example 3
Input: arr[] = [1, 2, 3, 4]
Output: 0
Explanation: We cannot trap water as there is no height bound on both sides.Example 4
π Constraints
β
My Approach
Two-Pointer Strategy
π Time and Auxiliary Space Complexity
π§βπ» Code (C)
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated