Skip to content
AI360Xpert
Beta
Difficulty: MediumTwo Pointers

Container With Most Water

Problem in Plain English

Given an array height where each entry marks the height of a vertical line, choose two lines that with the x-axis hold the most water and return that maximum area. A pair at distance w with shorter height h holds w * h units.

Problem Statement

You are given an integer array height of length n. The endpoints of line i are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container holding the most water, and return that maximum amount. The container may not be slanted, so a pair i < j holds min(height[i], height[j]) * (j - i) units.

Constraints

  • n == height.length
  • 2 <= n <= 10^5
  • 0 <= height[i] <= 10^4

Examples

Example 1

Input
height = [1,8,6,2,5,4,8,3,7]
Output
49

Lines 1 (height 8) and 8 (height 7) give min(8, 7) * (8 - 1) = 49, which is larger than every other pair.

Example 2

Input
height = [1,1]
Output
1

The only pair holds min(1, 1) * 1 = 1.

Example 3

Input
height = [0, 2, 0]
Output
0

Every pair includes a zero-height wall, so every container has zero area despite the tall middle line.

Example 4

Input
height = [5, 5, 5, 5]
Output
15

All pairs have limiting height 5. The endpoints give the greatest width, 3, and area 5 * 3 = 15; moving either pointer on a tie is safe.

Intuition

A container is limited by its shorter wall, so its area is width times the smaller height. Example 1 pairs index 1, height 8, with index 8, height 7: width 7 times height 7 gives 49. Begin with the widest pair. As width shrinks, improvement requires a taller limiting wall, so discard the shorter endpoint and keep looking inward. Lines between the endpoints do not subtract from the container area.

Approaches

Brute Force

Solution Details

Reveal Brute Force: intuition, complexity, and code

Hints

Hint 1
The area of a pair is set by the shorter line, so which side limits the current container?
Hint 2
Starting from the outermost pair gives the maximum width; what must improve to beat its area?
Hint 3
Only moving the shorter line inward can raise the limiting height, so the taller side can safely stay.

Edge Cases

  • Exactly two lines such as [1, 1], where the single pair is the answer.
  • Zero-height lines like [0, 2, 0], where any pair touching a 0 holds nothing.
  • Equal heights such as [5, 5, 5, 5], where the outermost pair wins and either pointer may move on ties.

Common Mistakes and Interview Tips

  • Advancing the taller line instead of the shorter one, which shrinks the width while the limiting height cannot grow.
  • Measuring height with max instead of min, which credits water the shorter wall cannot hold.
  • Enumerating all O(n2)O(n^2) pairs, which is impractical at n = 10^5.
  • Using j - i + 1 as width counts positions rather than the distance between walls.
  • Confusing this problem with trapped rainwater: intermediate lines do not subtract from the chosen container area.
  • Interview tip: prove which entire family of pairs is discarded by moving the shorter wall; a taller wall merely being possible is not the full correctness argument.

Key Takeaway

When candidates are ordered pairs and one move provably cannot improve the answer, converging pointers from both ends checks only the survivors in linear time.