Container With Most Water

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Container With Most Water

Approach

Start with two pointers at the widest possible container. Calculate the area using the shorter line as height. Move the pointer at the shorter line inward, since moving the taller line can only decrease width without increasing the limiting height.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Two Pointers (Converging)

Why It Works

The area is limited by the shorter line. Moving the shorter pointer inward is the only way to potentially find a taller line that could increase area, since decreasing width with the same height always reduces area.

Updated Feb 2026