Two Pointers · Medium

Container With Most Water

O(N) · O(1)

Initialize Two Pointers

Array is sorted. Set left pointer L=0 (val 1) and right pointer R=8 (val 7).

Array Elements & Pointers
L
1
L
8
[1]
6
[2]
2
[3]
5
[4]
4
[5]
8
[6]
3
[7]
R
7
R
Live Variables & Invariants
target:9
left:0
right:8
sum:8
Step 1 / 17
6%
Solution Code
1
function maxArea(height: number[]): number {
2
  let l = 0, r = height.length - 1, max = 0;
3
  while (l < r) {
4
    const area = Math.min(height[l], height[r]) * (r - l);
5
    max = Math.max(max, area);
6
    if (height[l] < height[r]) l++;
7
    else r--;
8
  }
9
  return max;
10
}
LeetCode IDE Console

Test Cases

3 cases from Blind 75 & NeetCode 150

Ask for a hint whenever you get stuck.