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 heightThis 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^50 <= height[i] <= 10^4
Approach 1 : Brute Force
Explanation
The simplest way to solve this problem is:
- Generate all possible pairs
- Calculate water stored between every pair
- Track maximum area
This approach is easy to understand but checking all pairs increases time complexity.
Steps
- Traverse all pairs.
- Calculate:
- width
- minimum height
- area
- Update maximum area.
- Return final answer.
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:
- Place:
- left pointer at beginning
- right pointer at end
- Calculate current area.
- Move the pointer having smaller height because:
- smaller height limits the area
This efficiently explores better containers.
Steps
- Initialize:
- left = 0
- right = n - 1
- While left < right:
- calculate area
- update maximum area
- Move:
- smaller height pointer
- 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
- Array contains same heights
- Array size equals 2
- Heights contain zeros
- Maximum area formed at extremes
- Maximum area formed in middle
Why This Problem is Important
This problem helps in understanding:
- Two Pointer Technique
- Greedy optimization
- Area calculation logic
- Pointer movement strategy
- Efficient traversal
It is one of the most important two pointer interview problems.
Real-World Applications
Area optimization concepts are used in:
- Water storage systems
- Resource allocation systems
- Geometry optimization
- Graphics simulations
- Spatial analysis systems
Common Mistakes
- Moving wrong pointer
- Incorrect width calculation
- Forgetting minimum height logic
- Updating maximum area incorrectly
Interview Tips
Interviewers often expect:
- O(n) two pointer solution
- Greedy reasoning
- Proper pointer movement explanation
Always explain why moving the smaller height pointer is necessary.
Related Questions
- Two Sum
- Trapping Rain Water
- Pair Sum
- Three Sum
- 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.