Skip to content
Optimal Lab

Two Pointers · 009Medium

Container With Most Water

Given an integer array height where each value is a vertical line at that index, find two lines that together with the x-axis form a container holding the most water. Return the maximum area. You may not slant the container.

Constraints

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

Examples

maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])  ->  49
maxArea([1, 1])                       ->  1
maxArea([0, 2])                       ->  0   // a zero-height line holds nothing
maxArea([1, 2, 1])                    ->  2   // the two outer 1s beat any taller-but-closer pair
maxArea([4, 4, 4, 4])                 ->  12   // all equal: width wins
maxArea([2, 3, 4, 5, 18, 17, 6])      ->  17   // min(18, 17) * 1

Edge cases

  • Zero-height lines contribute no area.
  • Exactly two lines.
  • Area is min(h[l], h[r]) * (r - l), not the sum.
Show the target complexity (spoiler)
Naive
O(n^2) time — evaluate every pair of lines.
Target
O(n) time, O(1) space — two pointers at the ends, always moving the shorter line inward.

Solve it

By hand, in your editor. The runner checks answers and timing.

git clone https://github.com/sarmadkung/optimal-round
cd optimal-round
# open problems/02-two-pointers/009-container-with-most-water.js
./practice c 009

Stuck? Watch the pattern run