codebrew.ai

Container With Most Water

Move the shorter wall inward — it's the only move that can help.

Approach

Two pointers at the ends bound the widest container. Area is limited by the shorter wall, so moving the taller one can never increase area; move the shorter pointer inward instead, tracking the max. One pass, no extra space.

Time complexity

O(n)

Space complexity

O(1)

Common mistake

Moving the taller line, or brute-forcing all O(n²) pairs instead of the greedy two-pointer shrink.

See it run, step by step

Generate an interactive lesson for Container With Most Water — trace every variable and watch the algorithm execute until it clicks.

Start a lesson on Container With Most Water

Related problems