Blind 75
11 / 75Free practice

Container With Most Water

Medium · Two pointers

Jump to code

Vertical lines stand at consecutive integer x-coordinates with nonnegative heights. Choose two lines that hold the greatest area of water with the x-axis; the water height is the shorter line.

Examples

Example 1

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

Output: 49

Example 2

height = [1,1]

Output: 1

Constraints

  • 2 <= height.length <= 1000
  • 0 <= height[i] <= 1000
Need a hint?

Moving the taller side cannot improve the area while the shorter side stays fixed.

Asked in real interviews

Candidates reported versions of Container With Most Water from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 7 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    1,
    8,
    6,
    2,
    5,
    4,
    8,
    3,
    7
  ]
]
Expected
49
Case 2
Input
[
  [
    1,
    1
  ]
]
Expected
1
Case 3
Input
[
  [
    4,
    3,
    2,
    1,
    4
  ]
]
Expected
16
Case 4
Input
[
  [
    1,
    2,
    1
  ]
]
Expected
2
Case 5
Input
[
  [
    1,
    2,
    3,
    4,
    5
  ]
]
Expected
6

Drafts are saved per language in this browser when storage is available.