mediumArrayTwo PointersTwo Pointers

Container With Most Water

mediumTime: O(n)Space: O(1)

Signals to notice

maximize area between two linesheights as barswidth vs height tradeoff

Brute force first

Check every pair of lines. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

The key insight

Two pointers at edges, move the shorter line inward. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

What must stay true

Moving the taller line can only decrease area (width shrinks, height can't improve), so always move the shorter one. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Easy way to go wrong

Moving the taller line — you should always move the shorter one to potentially find a taller line. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Two Pointers Pattern