AlgoMaster Logo

Container With Most Water

Last Updated: March 21, 2026

medium
3 min read

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 key observation here is that the area depends on both width and height. A wide container with short walls might hold more water than a narrow container with tall walls, or vice versa. We need to find the right balance.

Key Constraints:

  • 2 <= n <= 10^5 → We need O(n log n) or better. An O(n^2) brute force will be too slow.
  • 0 <= height[i] <= 10^4 → Heights are non-negative, so we don't need to worry about negative values.

Approach 1: Brute Force

Intuition

The most straightforward approach is to check every possible pair of lines and compute the area for each. We pick the pair that gives the maximum area. There are n*(n-1)/2 pairs, so we try them all.

This is the natural starting point. If someone asked you to find the best container and you had unlimited time, you'd just measure every combination and pick the winner.

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

This approach works but is too slow for the given constraints. What if we started with the widest container and strategically narrowed our search, skipping pairs that can't possibly beat our current best?

Approach 2: Two Pointers (Optimal)

Intuition

Here's the key insight: start with two pointers at the far ends of the array. This gives us the maximum possible width. Now, as we move pointers inward, the width decreases. So the only way to find a larger area is to find a taller minimum height.

But which pointer should we move? Think about it this way. The area is limited by the shorter of the two lines. If we move the taller line's pointer inward, the width shrinks and the height can't increase (it's still capped by the shorter line that didn't move). So the area can only decrease or stay the same. There's zero upside.

On the other hand, if we move the shorter line's pointer, we lose width, but we might find a taller line. The height was capped by this shorter line anyway, so replacing it with something taller could increase the area enough to overcome the width loss.

This greedy choice, always moving the pointer at the shorter line, guarantees we never skip 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

1Initialize: L=0, R=8, maxArea=0
0
1
L
1
8
2
6
3
2
4
5
5
4
6
8
7
3
8
7
R
1/8

Code