advanced8 min read·Updated September 20, 2026
Trapping Rain Water Explained: Tracing [0,1,0,2,1,0,1,3,2,1,2,1]
Master the two-pointer trap with our elevation valley mental model. Walk through the classic [0,1,0,2,1,0,1,3,2,1,2,1] example step by step.
By Learnisim AI·Published September 20, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- Basic array manipulation
- Two-pointer technique fundamentals
Why
Why We Need a Smarter Way to Measure Trapped Rain Water
Phase 1: The Pooling Problem
Imagine looking at a cross-section of a rugged landscape after a heavy storm. Rain has fallen, and water has pooled in the depressions between uneven hills and ridges. Given the elevation map
height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1], how many total units of water remain trapped between those vertical barriers instead of spilling off the edges?If you inspect bar by bar, water does not depend merely on how tall the immediate neighbors are. A single drop of water at index 2 (with height 0) is bounded by the tall wall at index 1 (height
1.to its left and an even taller wall at index 3 (height
2.to its right. It can rise as high as the shorter of those two bounding walls.
Without a systematic mental model, calculating this feels overwhelming: you might try scanning left and right infinitely from every single index, or getting tripped up by valleys nested inside larger valleys. We need a way to look at height constraints globally without re-scanning the entire array at every single step.
Model
Building the Elevation Valley Mental Model
Phase 2: The Two-Wall Boundary Rule
To understand how water gets trapped in our elevation map
height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1], we have to stop looking at single bars and start looking at boundaries. Imagine pouring water over this entire terrain: any individual bar at index can only hold water if there is a taller bar somewhere to its left and another taller bar somewhere to its right.Water does not care about the immediate neighbor alone; it cares about the absolute tallest peaks flanking it on either side. Specifically, the water level sitting directly on top of bar is strictly limited by the height of the shorter of those two bounding peaks. If we define as the maximum height from the start up to index , and as the maximum height from index to the end, the trapped water at any single position is given by:
Looking at our elevation map, index 2 has a height of 0. Its highest obstacle to the left is 1, and its highest obstacle to the right is 3. The limiting boundary is , meaning index 2 can pool water up to height 1. Subtracting its own height of 0 leaves 1 unit of trapped water.
Worked example
Tracing the Two-Pointer Sweep on the Elevation Map
Phase 3: Walking the Elevation Array
Let us trace the two-pointer algorithm step by step on our elevation map
height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]. We maintain left pointer , right pointer , leftMax = 0, rightMax = 0, and ans = 0.Given
Try this: Trace steps:
1.Init: L=0, R=11, leftMax=0, rightMax=0, ans=0.
2.height[L]=0, height[R]=1. leftMax < rightMax -> L moves to 1, leftMax becomes max(0, height[1]) = 1.
3.height[L]=1, height[R]=1. leftMax <= rightMax -> L moves to 2, leftMax remains 1.
4.height[L]=0, height[R]=1. leftMax=1. Water added at index 2 = leftMax - height[2] = 1 - 0 = 1. ans becomes 1.
Practice
Predicting the Sweep on a Modified Elevation Map
Phase 4: Practice
Now that you have seen how the two-pointer sweep maintains running maximums and accumulates water across
[0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1], it is time to apply the exact same mechanical trace to a slight variation of the array.Take the original elevation map and alter its center valley by raising the lowest interior bars. Consider the modified elevation map height array:
[0, 1, 0, 2, 1, 2, 1, 3, 2, 1, 2, 1]. Notice that the bar at index 5, which was previously 0, is now 2.Work through the two-pointer initialization: set left pointer
L = 0, right pointer R = 11, leftMax = 0, rightMax = 0, and ans = 0. As L and R step inward, compare how the new height of 2 at index 5 alters the boundary conditions compared to the original trace.Apply
Applying the Bounded-Max Pattern to Adjacent Architectural Problems
Phase 5: Applying the Bounded-Max Pattern
The two-pointer elevation sweep we used to solve the array is not just an isolated algorithmic trick—it is a blueprint for any problem where a local resource is constrained by the minimum of two global extremes. Recall that at every step of our trace, the water height above column was strictly decided by . This exact boundary-governed pattern reappears in geometry, skyline layout, and financial reservoir simulations where capacity is limited by the lowest threshold of surrounding walls.
To see this transfer in action, consider a 2D container wall optimization task: given a set of vertical panels of varying heights, you must find two panels that, together with the x-axis, form a container holding the maximum possible water volume (the classic Container With Most Water problem). While that problem uses a greedy two-pointer reduction moving inward from the absolute edges based on height comparison, the underlying mental model remains identical to our rain water trace. You are constantly letting the shorter boundary dictate the active constraint, sacrificing the limiting side in hopes of finding a taller bottleneck.
When encountering a new spatial capacity puzzle, do not immediately jump to brute-force nested loops that check every pair of boundaries. Instead, ask three diagnostic questions:
1. Is the capacity at position determined by the nearest enclosing peaks, or by the absolute highest peaks to the left and right?
1. Is the capacity at position determined by the nearest enclosing peaks, or by the absolute highest peaks to the left and right?
2.Can we maintain running running-maximums as pointers converge, eliminating redundant lookaheads?
3.Does the invariant guarantee that moving the pointer on the strictly smaller side is always safe because the opposing side acts as a safe upper bound?
By mapping these questions back to our elevation map trace, you can safely port the two-pointer sweep to higher-dimensional or modified architectural layouts without rewriting your core logic.
FAQ
How much water is trapped in the example [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]?
This elevation map traps a total of 6 units of water by accumulating liquid at index valleys bounded by taller left and right bars.
Why use two pointers instead of precomputing left and right maximum arrays?
The two-pointer approach optimizes space complexity from O(n) to O(1) by dynamically evaluating the limiting boundaries from both ends inward.
What is the time and space complexity of the optimal trapping rain water solution?
The two-pointer sweep runs in O(n) time complexity and O(1) auxiliary space complexity.
What is the primary pitfall when implementing the two-pointer approach?
A common mistake is moving the pointer with the larger maximum height instead of the smaller one, which compromises the accuracy of the bounding wall.