Container With Most Water in DSA C - Time & Space Complexity
We want to find how fast the algorithm finds the container holding the most water.
How does the time taken grow as the number of lines (heights) increases?
Analyze the time complexity of the following code snippet.
int maxArea(int* height, int heightSize) {
int left = 0, right = heightSize - 1;
int max_area = 0;
while (left < right) {
int width = right - left;
int h = height[left] < height[right] ? height[left] : height[right];
int area = width * h;
if (area > max_area) max_area = area;
if (height[left] < height[right]) left++;
else right--;
}
return max_area;
}
This code finds the largest 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: At most, it runs once for each element, so about n times.
As the number of lines increases, the loop runs roughly once per line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 |
| 100 | About 100 |
| 1000 | About 1000 |
Pattern observation: The operations grow linearly with input size.
Time Complexity: O(n)
This means the time taken grows directly in proportion to the number of lines.
[X] Wrong: "Because there are two pointers, the time is O(n²)."
[OK] Correct: Each pointer moves only forward or backward once, so total steps are at most n, not n times n.
Understanding this linear time approach shows you can optimize by avoiding unnecessary checks, a key skill in problem solving.
"What if we used a nested loop to check every pair? How would the time complexity change?"
Practice
Container With Most Water ask you to find given an array of heights?Solution
Step 1: Understand the problem statement
The problem asks to find two lines that together with the x-axis form a container holding 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 formed between two lines representing container walls -> Option DQuick Check:
Max area = max water container [OK]
- Confusing max area with sum of heights
- Looking for minimum height only
- Counting lines instead of calculating area
Container With Most Water in C?Solution
Step 1: Understand pointer initialization
We start with left pointer at the start (index 0) and right pointer at the end (index n-1) of the array.Step 2: Check each option
int left = 0, right = n - 1; correctly initializes left=0 and right=n-1. Others use invalid or out-of-bound indices.Final Answer:
int left = 0, right = n - 1; -> Option CQuick Check:
Two pointers at array ends = int left = 0, right = n - 1; [OK]
- Starting pointers out of array bounds
- Using 1-based indexing in C arrays
- Swapping left and right initialization
height = {1, 8, 6, 2, 5, 4, 8, 3, 7}?
int maxArea(int* height, int heightSize) {
int left = 0, right = heightSize - 1, max_area = 0;
while (left < right) {
int area = (right - left) * (height[left] < height[right] ? height[left] : height[right]);
if (area > max_area) max_area = area;
if (height[left] < height[right]) left++; else right--;
}
return max_area;
}
int main() {
int height[] = {1,8,6,2,5,4,8,3,7};
int size = sizeof(height)/sizeof(height[0]);
printf("%d", maxArea(height, size));
return 0;
}Solution
Step 1: Initialize pointers and max_area
left=0, right=8, max_area=0. Calculate area each step and update max_area.Step 2: Trace iterations
left=0 (1), right=8 (7), area=8*1=8, max=8, left++; left=1 (8), right=8 (7), area=7*7=49, max=49; next areas smaller (e.g. 6*3=18, 5*8=40), max_area remains 49.Final Answer:
49 -> Option BQuick Check:
Max water container area = 49 [OK]
- Calculating area incorrectly as sum instead of width*height
- Not updating max_area properly
- Moving wrong pointer in the loop
Container With Most Water and select the fix:
int maxArea(int* height, int heightSize) {
int left = 0, right = heightSize - 1, max_area = 0;
while (left <= right) {
int area = (right - left) * (height[left] < height[right] ? height[left] : height[right]);
if (area > max_area) max_area = area;
if (height[left] < height[right]) left++; else right--;
}
return max_area;
}Solution
Step 1: Analyze loop condition
The loop uses while(left <= right), which allows left == right, causing zero width and unnecessary calculation.Step 2: Correct loop condition
Change condition to while(left < right) to avoid zero width and infinite loop risk.Final Answer:
Change while condition to left < right -> Option AQuick Check:
Loop must run while left < right [OK]
- Using <= instead of < in loop condition
- Incorrectly updating max_area by adding
- Changing pointer initialization unnecessarily
Solution
Step 1: Understand the constraint
The container must include the line at index 0, so left pointer stays fixed at 0.Step 2: Move right pointer from end to left
Move right pointer from the last index towards 1, calculating area with fixed left pointer to find max area.Final Answer:
Fix left pointer at 0 and move right pointer from end towards left -> Option AQuick Check:
Fix one pointer at 0, move other to find max area [OK]
- Moving left pointer instead of fixing it
- Using nested loops unnecessarily
- Applying binary search incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
