SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Algorithm Patterns OverviewFrequency CountersTwo SumValid AnagramGroup AnagramsFrequency Counters and Hash-Map LookupTwo PointersValid PalindromeTwo Sum IIContainer With Most WaterTwo-Pointer ReasoningSliding WindowMaximum Average Subarray ILongest Substring Without Repeating CharactersMinimum Size Subarray SumSliding-Window Reasoning
Algorithm Patterns

Container With Most Water

Use two pointers to maximize the area between vertical lines.

Try It Yourself

Use the editor below to measure containers formed by two lines.

Choose which pointer to move

The current area depends on the distance between the pointers and the shorter of their two heights. Decide which pointer can move without discarding a possible improvement.

Submit your implementation when it passes the examples.

Container With Most Water

mediumTwo Pointers · Greedy30 minLC 11
Problem

Each value in heights represents a vertical line at that index. Choose two lines that hold the greatest amount of water.

Return the maximum area. The area is the shorter height multiplied by the distance between the lines.

Example 1:

Input: heights = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49
Explanation: The lines of heights 8 and 7 at indices 1 and 8 form the largest container. Its width is 7 and the shorter line is 7 units tall, so the area is 7 × 7 = 49.

Example 2:

Input: heights = [1, 1]
Output: 1
Explanation: There is only one pair of lines. Its width and shorter height are both 1, so the area is 1 × 1 = 1.

Constraints

  • 2 ≤ heights.length ≤ 100,000
  • 0 ≤ heights[i] ≤ 10,000
0 attempts

Test results

Results appear here after a run.

Solution

For heights 2, 5, 1, 4, the first pair holds 6 units. Replacing the shorter left wall raises the area to 8.

Start with the widest possible container. Record its area, then move the pointer at the shorter line because keeping that shorter height cannot improve the area after the width shrinks.

For heights [2, 5, 1, 4], the outside walls give 3 × 2 = 6. Keeping the wall of height 2 would only narrow that container. Move left to height 5: the width is now 2 and the limiting height is 4, giving area 8. If the two endpoint heights tie, either one can be discarded after recording its area.

function maxArea(heights) {
  let left = 0;
  let right = heights.length - 1;
  let maximum = 0;

  while (left < right) {
    const width = right - left;
    const height = Math.min(heights[left], heights[right]);
    maximum = Math.max(maximum, width * height);

    if (heights[left] <= heights[right]) left++;
    else right--;
  }

  return maximum;
}

Big O notation

MeasureComplexityExplanation
TimeO(n)The two pointers move inward across the array once.
Auxiliary spaceO(1)The algorithm stores only pointer positions and the best area.

How is this practice?

Previous lessonTwo Sum IINext lessonTwo-Pointer Reasoning
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation