Container With Most Water

medium

Find two lines that form container holding most water

Container With Most Water

Key Insight

Area = min(heights) × width. Move shorter line inward to potentially increase area.

Step 1Setup
Area = min(1,7) × 8 = 8
left
1
0
8
1
6
2
2
3
5
4
4
5
8
6
3
7
right
7
8
converge

Start with widest container. Heights: 1 and 7.

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Container With Most Water

Area = min(heights) × width. Move shorter line inward to potentially increase area.

  1. Setup

    Start with widest container. Heights: 1 and 7.

  2. Move Shorter Side

    Left is shorter (1). Move left to find taller line.

  3. New Max

    Area 49 > 8. This is our new maximum!

  4. Continue

    Right is shorter (7). Move right. Continue until pointers meet.

  5. Result

    Maximum area found: 49