Algomination
Data Structures
AboutContact

Algomination

Learn algorithms and data structures through smooth, interactive visualizations.

SortingSearchingArray AlgorithmsData StructuresAboutContact

© 2026Algomination. Created by Omang Rawat & Rahul Soni.

Omang Rawat
Rahul Soni
All array algorithms

Trapping Rain Water

Time O(n)Space O(1)

Computes how much water is trapped between bars of an elevation map. Two pointers walk inward from both ends, resolving the shorter side first since its limiting wall is already known.

Left‑max 0Right‑max 0Water trapped 0
L
0
1
0
2
1
0
1
3
2
1
2
R
1
Two pointers start at both ends. The water over each bar is capped by the shorter surrounding wall.
Step 1 / 13
Wall Trapped water

About Trapping Rain Water

Computes how much water is trapped between bars of an elevation map. Two pointers walk inward from both ends, resolving the shorter side first since its limiting wall is already known.

Time complexity: O(n). Space complexity: O(1).

How it works, step by step

Core idea — Water above a bar is capped by the shorter of the tallest wall to its left and the tallest to its right. Two pointers track those running maxima from each end.

What each pass accomplishes — Whichever side currently has the shorter bar is safe to settle: its bounding wall on that side is already final. Add (sideMax − barHeight) units of water there and move that pointer inward. Each bar is resolved exactly once.

Use the interactive visualizer above to run Trapping Rain Water on your own input and watch every comparison, swap, and operation animate step by step — pause, scrub, or replay at any speed.