Home  Dsa   The contain ...

The Container With Most Water problem and solution in Java and Python

The Container With Most Water problem is a classic DSA (Data Structures & Algorithms) question based on the two-pointer technique, and it’s a favorite in FAANG interviews.

Let’s go step by step 👇


🧩 Problem Statement

You are given an array height[], where each element represents the height of a vertical line drawn on the x-axis.

The goal is to find two lines that together form a container, holding the maximum amount of water. The container’s area is determined by the distance between the two lines (width) and the height of the shorter line.

Example:

Input:  height = [1,8,6,2,5,4,8,3,7]
Output: 49

Explanation:


📉 Brute Force Approach (O(n²))

Try every pair (i, j) and calculate the area:

area = min(height[i], height[j]) * (j - i)

Keep track of the maximum.

Java (Brute Force)

public class ContainerMostWater {
    public static int maxArea(int[] height) {
        int maxArea = 0;
        for (int i = 0; i < height.length; i++) {
            for (int j = i + 1; j < height.length; j++) {
                int area = Math.min(height[i], height[j]) * (j - i);
                maxArea = Math.max(maxArea, area);
            }
        }
        return maxArea;
    }

    public static void main(String[] args) {
        int[] height = {1,8,6,2,5,4,8,3,7};
        System.out.println(maxArea(height)); // 49
    }
}

Optimized Approach (O(n)) — Two Pointer Technique

Idea:

Start with two pointers at both ends of the array:

This works because the area is limited by the shorter height, and moving the taller side inward cannot increase area.


✅ Java Solution (Optimal)

public class ContainerMostWater {
    public static int maxArea(int[] height) {
        int left = 0, right = height.length - 1;
        int maxArea = 0;

        while (left < right) {
            int area = Math.min(height[left], height[right]) * (right - left);
            maxArea = Math.max(maxArea, area);

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

        return maxArea;
    }

    public static void main(String[] args) {
        int[] height = {1,8,6,2,5,4,8,3,7};
        System.out.println(maxArea(height)); // 49
    }
}

✅ Python Solution (Optimal)

def maxArea(height):
    left, right = 0, len(height) - 1
    max_area = 0

    while left < right:
        area = min(height[left], height[right]) * (right - left)
        max_area = max(max_area, area)

        if height[left] < height[right]:
            left += 1
        else:
            right -= 1

    return max_area

# Example
print(maxArea([1,8,6,2,5,4,8,3,7]))  # Output: 49

🧠 Complexity Analysis

TypeComplexity
Time ComplexityO(n)
Space ComplexityO(1)
TechniqueTwo Pointers

🔹 Why Two-Pointer Works


📊 Visualization (Conceptually)

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

Left  -> height[1] = 8
Right -> height[8] = 7
Width = 7, Height = 7
Area = 7 * 7 = 49  ✅

💡 Variations

  1. Trapping Rain Water Problem – similar use of two pointers, but adds trapped water calculation.
  2. Max Water Between K Lines – extension of this concept.
  3. Histogram Problems – often use similar area or height-width reasoning.

Published on: Oct 11, 2025, 08:27 AM  
 

Comments

Add your comment