Introduction

The Container With Most Water problem involves finding two lines that together can hold the maximum amount of water.

Given an array height[] where each element represents the height of a vertical line, the task is to find two lines such that:

  • the container formed between them stores the maximum water

The amount of water stored is calculated using:

 Area = width × minimum height

This problem helps in understanding:

  •  Two Pointer Technique
  • greedy optimization
  • area calculation
  • efficient traversal strategies

Example

Input: height[] = [1,8,6,2,5,4,8,3,7]Output: 49
Explanation:
Choose heights 8 and 7.
Width = 7
Minimum Height = 7 Area = 7 × 7 = 49
Input: height[] = [1,1] Output: 1 Explanation: Width = 1
Height = 1 Area = 1

Constraints

 2 <= n <= 10^5 0 <= height[i] <= 10^4

Approach 1 : Brute Force

Explanation

The simplest way to solve this problem is:

  1. Generate all possible pairs
  2. Calculate water stored between every pair
  3. Track maximum area

This approach is easy to understand but checking all pairs increases time complexity.

Steps

  1.  Traverse all pairs.
  2. Calculate:
    • width
    • minimum height
    • area
  3. Update maximum area.
  4. Return final answer.
Dry Run

Input Array:[1,8,6,2,5,4,8,3,7]
Check heights 1 and 8:
Width = 1
Area = 1 × 1 = 1
Check heights 8 and 7: Width = 7
Minimum Height = 7 Area = 7 × 7 = 49 Maximum Area = 49 Continue checking remaining pairs... Final Result:
49

Brute Force Code

Complexity Analysis

Time Complexity: O(n²)Explanation:
Every possible pair is checked.
Space Complexity: O(1)
Explanation:
No extra data structures are used.

Approach 2 : Optimized Solution (Two Pointer Technique)

Explanation

Instead of checking all pairs, we can use the Two Pointer Technique.

The idea is:

  1. Place:
    • left pointer at beginning
    • right pointer at end
  2. Calculate current area.
  3. Move the pointer having smaller height because:
    • smaller height limits the area

This efficiently explores better containers.

Steps

  1. Initialize:
    • left = 0
    • right = n - 1
  2. While left < right:
    • calculate area
    • update maximum area
  3. Move:
    • smaller height pointer
  4. Return maximum area.

Dry Run

Input Array:[1,8,6,2,5,4,8,3,7]
Initially:
left = 0 → 1
right = 8 → 7
Width = 8
Minimum Height = 1 Area = 8 Maximum Area = 8 Move left pointer left = 1 → 8
right = 8 → 7 Width = 7 Minimum Height = 7 Area = 49 Maximum Area = 49 Move right pointer Continue traversal... Final Result:
49

Optimized Code

Complexity Analysis

Time Complexity: O(n)Explanation:
Each pointer moves at most once across the array.

Space Complexity: O(1)
Explanation:
No extra data structures are used.

Edge Cases

  1. Array contains same heights
  2. Array size equals 2
  3. Heights contain zeros
  4. Maximum area formed at extremes
  5. Maximum area formed in middle

Why This Problem is Important

This problem helps in understanding:

  1. Two Pointer Technique
  2. Greedy optimization
  3. Area calculation logic
  4. Pointer movement strategy
  5. Efficient traversal

It is one of the most important two pointer interview problems.

Real-World Applications

Area optimization concepts are used in:

  1. Water storage systems
  2. Resource allocation systems
  3. Geometry optimization
  4. Graphics simulations
  5. Spatial analysis systems

Common Mistakes

  1. Moving wrong pointer
  2. Incorrect width calculation
  3. Forgetting minimum height logic
  4. Updating maximum area incorrectly

Interview Tips

Interviewers often expect:

  1. O(n) two pointer solution
  2. Greedy reasoning
  3. Proper pointer movement explanation

Always explain why moving the smaller height pointer is necessary.


Related Questions

  1. Two Sum
  2. Trapping Rain Water
  3. Pair Sum
  4. Three Sum
  5. Maximum Subarray

Final Takeaway

The Container With Most Water problem is a fundamental two pointer problem that teaches greedy optimization and efficient pointer movement strategies. Understanding this problem builds a strong foundation for advanced optimization and traversal interview problems.