Two Pointers/Medium

Container With Most WaterPRO PASS

Time: O(N)Space: 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 / 176%
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
}

Custom Test Case Runner

Input your custom values and visualize step-by-step trace

Quick Presets:

AI DSA Coach

Contextual Tutor for Container With Most Water

Hello! I am your AI DSA Tutor for **Container With Most Water** (Two Pointers). Ask me anything about this algorithm, time complexity, or request a step hint!