intermediate8 min read·Updated September 19, 2026
Container With Most Water Explained: Tracing [1, 8, 6, 2, 5, 4, 8, 3, 7]
Master the container with most water problem by tracing height [1,8,6,2,5,4,8,3,7] to 49. Learn the two-pointer mental model, O(n) logic, and edge cases.
By Learnisim AI·Published September 19, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- basic array indexing
- understanding of O(n) time complexity
Why
Why Brute Force Fails on the Container With Most Water Problem
Phase 1: The Trap of Pairwise Combinations
Imagine you are handed an array of vertical wall heights: . Your goal is to pick two walls that, together with the horizontal x-axis, form a basin capable of holding the maximum possible volume of water. The area of water held between any two indices and is strictly bounded by the shorter wall multiplied by the distance between them: .
If you approach this with a naive mindset, you might think: "Why not just test every single pair of walls?" With walls, there are unique pairs. For our array of length 9, that means checking 36 combinations. But what if ? Checking every pair balloons into billions of operations, causing your code to time out.
Without a smarter strategy, you find yourself pointlessly recalculating areas for pairs that are obviously too narrow or too short to ever beat your current record. We need a way to strategically discard impossible pairs without ever actually computing their area.
Model
Building the Two-Pointer Geometric Model
Phase 2: The Geometry of Pointers
When we look at our locked working example heights , we are trying to maximize the area of a rectangle. The area formula depends strictly on two things: the horizontal distance between our left and right lines, and the limiting height of the shorter line.
If we start with our pointers at the extreme ends of the array, (height 1) and (height 7), our width is at its absolute maximum of . However, our height is bottlenecked by , yielding an initial area of .
To find a larger container, we cannot just guess randomly; we need a systematic elimination rule. Since the width shrinks by 1 unit with every step we take inward, the only way to find a larger area is to find a significantly taller limiting boundary. This physical constraint gives rise to the greedy two-pointer reduction model.
Worked example
Tracing the Two-Pointer Algorithm on the Target Array
Phase 3: Worked Example
Now that we have established our two-pointer geometric model, let us trace it step-by-step through our locked array. We want to see how the pointers narrow down the search space without missing the optimal container, maintaining a running maximum area along the way.
Given
* Heights array: of length .
* Initial Pointers: Left pointer at height 1, Right pointer at height 7.
* Initial Pointers: Left pointer at height 1, Right pointer at height 7.
•Initial Max Area: 0.
Steps
1.Iteration 1:
* (), ().
* Width = .
* Height limited by left pointer: .
* Current area = .
* Update max area: .
* Since , increment to 1.
2.Iteration 2:
* (), ().
* Width = .
* Height limited by right pointer: .
* Current area = .
* Update max area: .
* Since , decrement to 7.
3.Subsequent iterations:
•The pointers continue moving inward, comparing heights and shrinking the width. For instance, pairing the two 8s at indices 1 and 6 gives width 5 and height 8, yielding 40, which is smaller than our current maximum of 49.
Result
The algorithm terminates when meets at index 4, having successfully identified the maximum possible trapped water area of 49 between indices 1 and 8.
Try this: Given L = 1 (height 8) and R = 7 (height 3), calculate the width, limiting height, and resulting area for this specific step.
Practice
Predicting the Path for a New Configuration
Phase 4: Practice
Now that you have traced the two-pointer dance on our original heights array
[1, 8, 6, 2, 5, 4, 8, 3, 7], let us see how the algorithm behaves when we alter the distribution. Consider a modified height array where the peak heights are shifted: [2, 3, 4, 5, 18, 17, 6].Imagine setting your left pointer at height 2 and your right pointer at height 6. Ask yourself which pointer will move on the very first step, and what the resulting container area will be after that first evaluation. Walk through the comparison of the boundary heights before updating your indices.
Apply
Recognizing Where the Two-Pointer Pattern Applies Beyond Water
Phase 5: Generalizing the Pattern
We started our journey with the locked array
[1, 8, 6, 2, 5, 4, 8, 3, 7] and watched our two pointers converge from width 8 down to 1, safely discarding suboptimal pairs without missing the maximum area of 49. That dramatic reduction from down to time did not rely on water or physics; it relied on a strict monotonic trade-off: moving the shorter boundary was the only way to potentially find a taller height that could compensate for a shrinking width.Now, imagine you are given a completely different scenario: searching for two numbers in a sorted array that sum up to a specific target value. Instead of maximizing area between vertical lines, you are tuning a sum. But notice how the mechanics rhyme with our container walkthrough. If the current sum is too small, which pointer must you advance to increase the sum? If the sum is too large, which pointer must you retreat? Recognizing this invariant allows you to take the exact same inward-shrinking rhythm we used on our heights array and apply it to sorted numerical search spaces.
To test this transfer of knowledge, consider how you would adapt the pointer-movement rule for a problem where you want to find two indices whose product equals a target, or where you need to check if a string is a palindrome by comparing outer characters inward. In each case, the underlying mental model remains identical: start at the widest possible boundaries, evaluate the objective function, and discard the side that guarantees no better outcome.
FAQ
How does the two-pointer approach achieve a maximum area of 49 in the example [1, 8, 6, 2, 5, 4, 8, 3, 7]?
By placing pointers at index 1 (height 8) and index 8 (height 7). The limiting height is min(8, 7) = 7, and the width is 8 - 1 = 7, yielding an area of 49. Moving the pointer with the taller height would only decrease width without any guarantee of finding a taller boundary.
Why do we always move the pointer pointing to the shorter vertical line?
The area is constrained by the shorter line and the distance between pointers. Moving the taller line's pointer can only decrease the width while keeping or lowering the bottleneck height, guaranteeing a smaller or equal area. To find a potentially larger area, we must give the shorter line a chance to be replaced by a taller one.
What is the time and space complexity of the two-pointer solution?
The time complexity is O(n) because each element is visited at most once as the left and right pointers move inward. The space complexity is O(1) since we only use a few constant variables to track the pointers and maximum area.