Container With Most Water

13 May 2026 0 Likes

Problem Description

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container such that the container holds the maximum amount of water.

Return the maximum amount of water a container can store. The amount of water a container can store is determined by: Width Ɨ Minimum Height
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The container formed by heights 8 and 7 stores the maximum water.

Width = 7
Minimum Height = 7

Area = 7 Ɨ 7 = 49
Example 2:
Input: height = [1,1]
Output: 1
Explanation:
Width = 1
Minimum Height = 1

Area = 1 Ɨ 1 = 1
Example 3:
Input: height = [4,3,2,1,4]
Output: 16
Explanation: The first and last lines form the largest container.

Width = 4
Minimum Height = 4

Area = 4 Ɨ 4 = 16

Constraints

The array contains at least 2 elements. Each value inside height represents a non-negative integer. The answer should represent the maximum amount of water that can be stored between any two lines.

Efficient solutions are expected to avoid checking every possible pair of lines unnecessarily and should aim for linear time complexity.

Understanding the Problem

The problem is asking us to select two vertical lines such that the area formed between them becomes maximum. The amount of water stored depends on two factors:

1. The distance between the two lines (width)
2. The shorter line among them (minimum height)

For example, consider: height = [1,8,6,2,5,4,8,3,7]

If we choose the lines with heights 8 and 7, the width between them becomes 7, while the smaller height becomes 7. Therefore: Area = 7 Ɨ 7 = 49

Even if one line is extremely tall, the shorter line always limits the amount of water the container can hold. The main challenge is efficiently finding the best pair without checking every possible combination of lines.

Hints

The first hint is to think about how the container area changes when the width decreases. Since the area depends on the shorter height, moving the taller line usually does not help improve the result.

A useful observation is that the smaller height becomes the limiting factor. Therefore, while using two pointers, it is generally beneficial to move the pointer pointing to the shorter line because doing so may increase the minimum height and potentially produce a larger area.

Using a two-pointer technique allows efficient traversal from both ends of the array while continuously updating the maximum area found so far.

Solutions

BRUTE FORCE

Nested Loop Container Area Calculation Solution

This approach checks every possible pair of lines and calculates the amount of water that can be stored between them. For every pair of indices i and j, we first determine the container height using the smaller of the two heights because the shorter wall always limits the amount of water stored.

Next, we calculate the width using: j - i. The container area then becomes: Minimum Height Ɨ Width

For example, if: height[i] = 8, height[j] = 7 and width = 7, then Area = 7 Ɨ 7 = 49.

We repeat this calculation for every possible pair and continuously track the maximum area found so far.

Although this solution is straightforward and easy to understand, it becomes inefficient for large arrays because every pair of lines is checked explicitly.
public class A_BruteForce {
    public int maxArea(int[] heights) {
        int maxArea = 0;
        int n = heights.length;

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                int containerHeight = Math.min(heights[i], heights[j]);
                int containerWidth = j - i;

                int currentArea = containerHeight * containerWidth;

                if (currentArea > maxArea) {
                    maxArea = currentArea;
                }
            }
        }
        return maxArea;
    }
}
class Solution:

    def maxArea(self, heights):

        max_area = 0

        n = len(heights)

        for i in range(n):

            for j in range(i + 1, n):

                container_height = min(
                    heights[i],
                    heights[j]
                )

                container_width = j - i

                current_area = (
                    container_height * container_width
                )

                max_area = max(
                    max_area,
                    current_area
                )

        return max_area
Time Complexity
O(n²)
Space Complexity
O(1)
OPTIMAL

Two Pointer Maximum Area Solution

This approach uses the two-pointer technique to efficiently find the maximum container area without checking every possible pair. We start with one pointer at the beginning of the array and another pointer at the end because this gives the maximum possible width initially.

At every step, we calculate the current container area using: Minimum Height Ɨ Width, where: Minimum Height is the smaller of the two boundary heights, and Width is the distance between the pointers.

After calculating the area, we update the maximum area found so far.

The key observation is that the shorter line always limits the current container. Therefore, moving the taller line cannot increase the area because the width decreases while the limiting height remains unchanged.

To potentially find a better container, we move the pointer pointing to the shorter line because a taller height may appear ahead and compensate for the reduced width.

This intelligent pointer movement avoids unnecessary comparisons and allows the solution to process the array in linear time.
class Solution:

    def maxArea(self, heights):

        max_area = 0

        left = 0
        right = len(heights) - 1

        while left < right:

            current_height = min(
                heights[left],
                heights[right]
            )

            width = right - left

            current_area = (
                current_height * width
            )

            max_area = max(
                max_area,
                current_area
            )

            if heights[left] < heights[right]:

                left += 1

            else:

                right -= 1

        return max_area
public class B_TwoPointer {
    public int maxArea(int[] heights) {
        int maxArea = 0;
        int left = 0;
        int right = heights.length - 1;

        while (left < right) {
            // Calculate the limiting height and the distance between pointers
            int currentHeight = Math.min(heights[left], heights[right]);
            int width = right - left;

            int currentArea = currentHeight * width;
            maxArea = Math.max(maxArea, currentArea);

            // Move the pointer that points to the shorter wall to potentially find a taller one
            if (heights[left] < heights[right]) {
                left++;
            } else {
                right--;
            }
        }
        return maxArea;
    }
}
Time Complexity
O(n)
Space Complexity
O(1)

Conclusion

The Container With Most Water problem is one of the most important interview problems involving the two-pointer technique. The problem teaches how intelligent pointer movement can drastically reduce unnecessary computations compared to brute-force approaches.

Mastering this problem helps build a strong understanding of greedy decision making, pointer-based traversal, and area optimization strategies, which are commonly used in coding interviews and real-world algorithmic problems.

šŸ’¬ Comments