Skip to main content
Back to problems
Leetcode
Medium
Arrays
Two Pointers
Geometry
Google
Container With Most Water

Find two vertical lines that form a container holding the maximum amount of water.

Acceptance 96%
Problem Statement

Container With Most Water

You are given an array of non-negative integers where each value represents the height of a vertical line drawn at that index. Choose any two lines together with the x-axis to form a container.

Return the maximum amount of water the container can hold.

The water contained by two lines at positions ii and jj is determined by:

  • the distance between them, and
  • the shorter of the two heights.

So the area is:

area=(ji)×min(h[i],h[j])\text{area} = (j - i) \times \min(h[i], h[j])

Find the largest possible area over all valid pairs.

Input Format

  • An integer array height of length n
  • height[i] is the height of the vertical line at index i

Output Format

  • Return a single integer: the maximum container area

Constraints

  • 2n2 \le n
  • 0height[i]0 \le height[i]
  • Use 64-bit arithmetic if your language may overflow on large products
Examples
Sample cases returned by the problem API.

Example 1

Input

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

Output

49

Explanation

The best pair is the lines at indices 1 and 8 with heights 8 and 7. The width is 7, so the area is 7 × 7 = 49.

Example 2

Input

height = [1,1]

Output

1

Explanation

Only one pair exists. The width is 1 and the limiting height is 1, so the area is 1.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.