Container With Most Water Visualization & Animation

Finds two lines forming the largest water container using two pointers that always move the shorter line inward.

## What is it? Find two vertical lines in a histogram that together with the x-axis form a container holding the most water. The amount of water is `min(height[left], height[right]) * (right - left)`. ## How it works - Place `left = 0`, `right = n-1` - Compute area = `min(heights[left], heights[right]) * (right - left)`, update max - Move the pointer at the shorter line inward (the taller line can never form a larger container by moving) - Repeat until `left >= right` ## When to use - Maximizing area between two lines (LeetCode 11) - Any problem involving a "measure between two boundaries" optimization - Two-pointer greedy on sorted or unsorted arrays ## Key Points - O(n) time, O(1) space - Greedy insight: moving the taller pointer never helps (width decreases and height stays ≤ current); moving the shorter one might - The two-pointer approach proves optimality — it considers all potentially optimal pairs

Category: algorithms

Difficulty: intermediate

Time Complexity: O(n)

Space Complexity: O(1)

Container With Most Water

intermediate

Finds two lines forming the largest water container using two pointers that always move the shorter line inward.

We place two pointers — L at the leftmost bar (height 1) and R at the rightmost bar (height 7). Each step we calculate how much water fits between them, then move the shorter bar inward to try to find a bigger container.