Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Use two pointers from both ends, tracking the maximum height seen from each side. Process the pointer with the smaller max height: if the current bar is shorter than its side max, the difference is trapped water. Otherwise, update the side max. Move the processed pointer inward.
Water at any position is determined by min(leftMax, rightMax) - height. By always processing the side with the smaller max, we know the other side has a taller or equal wall, so the water level is determined solely by the current side max.
Updated Feb 2026