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
Container With Most Water — Two-Pointer Optimization Array = [1, 8, 6, 2, 5, 4, 8, 3, 7] | Max Area = 49 (Indices 1 to 8) Active Iteration: L=1 (h=8) to R=8 (h=7) 0 1(L) 2 3 4 5 6 7 8(R) Width = 7 Limit: min = h[8]=7 Area = 7 × 7 = 49 1. Area Formula & Greedy Rule Area = (R - L) × min(h[L], h[R]) • Always move pointer with smaller height. • Maximizes chance of finding a taller wall. 2. Brute Force vs. O(n) • Pairs checked: n(n-1)/2 = 36 checks • Two-Pointer O(n): visits each wall at most once (9 total steps). 3. Step-by-Step Trace • Iter 1 (L=0, R=8): Area = 8 × 1 = 8 • Iter 2 (L=1, R=8): Area = 7 × 7 = 49 (Max!) • Subsequent moves narrow toward center. Result: Maximum trapped water = 49
Max water in height = [1, 8, 6, 2, 5, 4, 8, 3, 7] overview diagram
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.
The Trap of Pairwise Combinations (Brute Force Fails) Array h = [1, 8, 6, 2, 5, 4, 8, 3, 7] • Total Unique Pairs = n(n-1)/2 = 36 Checks Visualizing Water Area: Area = min(h_L, h_R) × (R - L) 1 8 6 2 5 4 8 3 7 Water Volume: min(8,7) × 7 = 49 L (idx 1) R (idx 8) Why Brute Force Explodes O(n²) 1. Nested Loops (i from 0 to n-1, j from i+1 to n-1) Calculates every possible basin: (0,1), (0,2) ... (7,8) 2. Redundant Computations Re-checks narrow or tiny walls that can't beat max 3. Time Limit Exceeded for Large N • n = 100,000 → ~5,000,000,000 inner checks • Solution: Need Two Pointers moving inward O(n) Key Insight: Discard the shorter wall without checking inner pairs!
Why Brute Force Fails on the Container With Most Water Problem diagram
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.
Container With Most Water: Two-Pointer Geometric Model Heights: [1, 8, 6, 2, 5, 4, 8, 3, 7] • Area = (R - L) × min(hL, hR) Initial Water Area = 8 × 1 = 8 1 L=0 1 2 3 4 5 6 7 8 Width = R - L = 8 - 0 = 8 min(hL, hR) = 1 1. The Area Formula Area = (R - L) × min(hL, hR) Width shrinks by 1 unit with every single step. 2. The Bottleneck Dilemma Initial height is capped at min(1, 7) = 1. Moving the taller line (R=8) guarantees height can only get smaller or stay same! 3. The Greedy Reduction Rule Always shift the pointer at the SHORTER line (here L=0, height 1). → Only way to find a larger area! Progression: Start: (0, 8) Shift L++: (1, 8) Area = 7 × 8 = 56 O(n) Optimal Scan
Building the Two-Pointer Geometric Model diagram
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 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.
Phase 3: Two-Pointer Worked Example ([1, 8, 6, 2, 5, 4, 8, 3, 7]) Current Area = 49 (Width 7 × Height 7) 1 8 (L) 6 2 5 4 8 3 1 8 (L) 6 2 5 4 8 3 7 (R) 0 1 2 3 4 5 6 7 8 Width = 7 Iteration 2 State L = 1 (height 8) R = 8 (height 7) Area = 7 × 7 = 49 Max Area: 8 → 49 8 > 7 ⇒ Decrement R Pointers converge inward, maintaining running maximum area (49 found between indices 1 & 8)
Tracing the Two-Pointer Algorithm on the Target Array diagram
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.
python
def test_modified_container():
    heights = [2, 3, 4, 5, 18, 17, 6]
    # Given L = 0 (val 2) and R = 6 (val 6):
    # 1. What is the initial area?
    # 2. Which pointer increments/decrements?
    pass
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.
python
def generalized_transfer_check(arr, target):
    # TODO: Apply the outer-inward pointer convergence pattern
    # learned from the container problem to a new search domain.
    pass

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.

Keep learning