Container With Most Water in DSA Java - Time & Space Complexity
We want to understand how the time needed to find the container with the most water changes as the number of lines increases.
How does the number of operations grow when we have more lines to check?
Analyze the time complexity of the following code snippet.
public 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;
}
This code finds the maximum water container by moving two pointers from both ends towards the center.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves pointers inward.
- How many times: The loop runs at most once for each element, so about n times where n is the number of lines.
As the number of lines increases, the loop runs roughly once per line, so the operations grow in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 |
| 100 | About 100 |
| 1000 | About 1000 |
Pattern observation: The operations increase directly with input size, doubling input roughly doubles work.
Time Complexity: O(n)
This means the time to find the max container grows linearly with the number of lines.
[X] Wrong: "Because we check pairs of lines, the time is O(n²)."
[OK] Correct: The two-pointer method moves inward without checking all pairs, so it only does about n steps, not n squared.
Understanding this linear time approach shows you can optimize from a simple but slow method to a smart, efficient one, a key skill in problem solving.
"What if we used a nested loop to check every pair of lines? How would the time complexity change?"
Practice
Container With Most Water problem ask you to find given an array of heights?Solution
Step 1: Understand the problem statement
The problem asks to find two lines which together with the x-axis form a container that holds the most water.Step 2: Identify what to maximize
We want to maximize the area between two lines, which is the width between them times the smaller height.Final Answer:
The maximum area of water that can be contained between two lines. -> Option BQuick Check:
Max area = max water container [OK]
- Confusing maximum area with sum of heights
- Ignoring the smaller height between two lines
- Thinking it's about counting lines
Container With Most Water problem in Java?Solution
Step 1: Understand array indexing in Java
Arrays start at index 0, so the left pointer should start at 0.Step 2: Set right pointer to last index
The right pointer should be at the last element, which is length - 1.Final Answer:
int left = 0, right = heights.length - 1; -> Option DQuick Check:
Array indices start at 0 [OK]
- Starting pointers at 1 instead of 0
- Using length instead of length - 1 for last index
- Confusing array length with last index
Container With Most Water problem?int[] heights = {1, 8, 6, 2, 5, 4, 8, 3, 7};
int left = 0, right = heights.length - 1;
int maxArea = 0;
while (left < right) {
int height = Math.min(heights[left], heights[right]);
int width = right - left;
maxArea = Math.max(maxArea, height * width);
if (heights[left] < heights[right]) {
left++;
} else {
right--;
}
}
System.out.println(maxArea);Solution
Step 1: Initialize pointers and maxArea
left=0, right=8, maxArea=0Step 2: Iterate and calculate areas
Calculate areas step-by-step: - left=0, right=8: min(1,7)=1, width=8, area=8, maxArea=8 - left=1, right=8: min(8,7)=7, width=7, area=49, maxArea=49 - left=1, right=7: min(8,3)=3, width=6, area=18, maxArea=49 - left=1, right=6: min(8,8)=8, width=5, area=40, maxArea=49 - left=1, right=5: min(8,4)=4, width=4, area=16, maxArea=49 - left=1, right=4: min(8,5)=5, width=3, area=15, maxArea=49 - left=1, right=3: min(8,2)=2, width=2, area=4, maxArea=49 - left=1, right=2: min(8,6)=6, width=1, area=6, maxArea=49 Loop ends when left >= right. This misses some pointer moves. Let's trace fully: - Start: left=0, right=8 area = min(1,7)*8=8 heights[left] left++ => left=1 - left=1, right=8 area = min(8,7)*7=49 heights[left]>heights[right] => right-- => right=7 - left=1, right=7 area = min(8,3)*6=18 heights[left]>heights[right] => right-- => right=6 - left=1, right=6 area = min(8,8)*5=40 heights[left]==heights[right] => right-- => right=5 - left=1, right=5 area = min(8,4)*4=16 heights[left]>heights[right] => right-- => right=4 - left=1, right=4 area = min(8,5)*3=15 heights[left]>heights[right] => right-- => right=3 - left=1, right=3 area = min(8,2)*2=4 heights[left]>heights[right] => right-- => right=2 - left=1, right=2 area = min(8,6)*1=6 heights[left]>heights[right] => right-- => right=1 - left=1, right=1 loop ends Max area found is 49. But the maximum area for this input is actually 49. However, 56 is 56, which is incorrect. Let's check if 56 is possible: - Between indices 1 and 6: min(8,8)*5=40 - Between indices 1 and 8: min(8,7)*7=49 - Between indices 2 and 8: min(6,7)*6=36 - Between indices 1 and 7: min(8,3)*6=18 - Between indices 0 and 8: min(1,7)*8=8 No 56 found. Therefore, correct answer is 49 -> 49.Final Answer:
49 -> Option AQuick Check:
Max area found = 49 [OK]
- Calculating area with max height instead of min
- Not updating maxArea correctly
- Moving wrong pointer in the loop
Container With Most Water problem:int[] heights = {1, 2, 4, 3};
int left = 0, right = heights.length - 1;
int maxArea = 0;
while (left <= right) {
int height = Math.min(heights[left], heights[right]);
int width = right - left;
maxArea = Math.max(maxArea, height * width);
if (heights[left] < heights[right]) {
left++;
} else {
right--;
}
}
System.out.println(maxArea);Solution
Step 1: Analyze loop condition
The loop runs while left <= right, which allows left == right, meaning width = 0.Step 2: Understand impact on area calculation
When left == right, width = 0, so area is zero and unnecessary iteration occurs. The correct condition is left < right to avoid zero width.Final Answer:
The loop condition should be left < right, not left <= right. -> Option CQuick Check:
Loop must stop before pointers meet [OK]
- Using <= instead of < in loop condition
- Incorrect width calculation
- Wrong initialization of maxArea
Solution
Step 1: Adjust pointers to even indices
Start left pointer at index 0 (first even index) and right pointer at the last even index (length-1 or length-2 depending on parity).Step 2: Move pointers inward skipping odd indices
When moving pointers, increment or decrement by 2 to stay on even indices only.Step 3: Calculate area as usual
Calculate area using min height and width between pointers, update max area accordingly.Final Answer:
Initialize pointers at the first and last even indices, then move inward skipping odd indices. -> Option AQuick Check:
Restrict pointers to even indices [OK]
- Using standard two-pointer without skipping odd indices
- Sorting array which changes line positions
- Using nested loops causing higher complexity
Ready to prove it?
Pass the test and this pattern moves unknown
