AlgoMaster Logo

Container With Most Water

mediumFrequency5 min readUpdated June 23, 2026

Understanding the Problem

We're looking at vertical lines on a 2D plane. Each line is positioned at index i with a height of height[i]. If we pick any two lines, they form the sides of a container. The water this container can hold depends on two things: the distance between the two lines (the width), and the shorter of the two lines (the height, since water would overflow past the shorter line).

So for any pair of lines at indices i and j, the area is:

The question is: which pair gives us the maximum area?

The area depends on both factors at once. A wide container with short walls can hold more water than a narrow container with tall walls, or the other way around, so neither width nor height alone decides the answer.

Key Constraints:

  • 2 <= n <= 10^5 → There are about n^2/2, or 5 billion, pairs at the upper bound, so an O(n^2) scan over all pairs is too slow. We need something close to O(n).
  • 0 <= height[i] <= 10^4 → The largest possible area is 10^4 * (10^5 - 1), which is below 10^9 and fits in a 32-bit signed integer. No long arithmetic is needed.

Approach 1: Brute Force

Intuition

Check every possible pair of lines, compute the area for each, and keep the largest. There are n*(n-1)/2 pairs, and evaluating all of them is guaranteed to find the answer. This establishes a correct baseline before any optimization.

Algorithm

  1. Initialize a variable maxArea to 0.
  2. For each line at index i from 0 to n-2:
    • For each line at index j from i+1 to n-1:
      • Calculate area = min(height[i], height[j]) * (j - i).
      • Update maxArea if this area is larger.
  3. Return maxArea.

Example Walkthrough

1Initialize: maxArea=0, check every pair (i, j)
0
1
i
1
j
8
2
6
3
2
4
5
5
4
6
8
7
3
8
7
1/8

Code

At n = 10^5 this is around 5 billion pair evaluations, far beyond the time limit. The next approach starts from the widest possible container and discards pairs in bulk instead of measuring each one.

Approach 2: Two Pointers (Optimal)

Intuition

Start with two pointers at the far ends of the array, which gives the maximum possible width. Every move inward costs width, so a later pair can only win by having a taller minimum height.

That determines which pointer to move. The area is limited by the shorter of the two lines. Moving the taller line's pointer shrinks the width while the height stays capped by the shorter line that did not move, so the area can only decrease or stay the same. Moving the shorter line's pointer also costs width, but it can replace the limiting line with a taller one, and that is the only change that can raise the area.

Always moving the pointer at the shorter line is the greedy choice, and it never skips a pair that could be the answer.

Algorithm

  1. Place a left pointer at index 0 and a right pointer at index n-1.
  2. While left < right:
    • Calculate the area using the two lines at left and right.
    • Update maxArea if this area is larger.
    • Move the pointer pointing to the shorter line inward (if they're equal, move either one).
  3. Return maxArea.

Example Walkthrough

Loading simulation...

Code