THE WHY BEHIND EVERY DSA PROBLEM
SHEET: STRIVER A2Z · REV 0.1
UNDERSTOOD: 0 / 79 DRAFTED
STEP 3 · ARRAYS · TWO POINTERS · MEDIUM

Container With Most Water

WHAT IT SAYS

Pick two walls that hold the most water.

WHAT IT'S REALLY ASKING

"Which pairs can we prove are not worth checking?"

THE INSIGHT LADDER — FROM BRUTE FORCE TO OPTIMAL

Brute force: every pair of walls

O(n²) time · O(1) space

Check all n(n−1)/2 pairs, compute min(height) × width, keep the max.

WHERE THE WORK IS WASTED — Most pairs are doomed before you compute them: any pair reusing a short wall at a narrower width was never going to beat what you already have.

!
KEY OBSERVATION — THE UNLOCKlink

The shorter wall condemns all its remaining pairs.

Area = min(hL, hR) × width. Start at maximum width. Narrowing costs width for certain — it only pays off if the height cap rises. But the cap IS the shorter wall, so every remaining pair that keeps the shorter wall is strictly ≤ the container you just measured. Discarding them all at once isn't a guess. It's a proof.

Two pointers, always move the shorter

O(n) time · O(1) space

Start at both ends. Measure, then move whichever pointer sits on the shorter wall. Each step safely eliminates a whole family of pairs, so n−1 measurements cover all of them.

WHAT YOU TRADED — Two pointers isn't a trick here — it's an elimination argument. If you can't explain why the skipped pairs are safe to skip, you've memorized the movement, not the reason.
WATCH THE IDEA RUN
1
0L
8
1
6
2
2
3
5
4
4
5
8
6
3
7
7
8R
area 8
best 8
Area = min(1, 7) × 8 = 8. New best. The left wall (1) caps the height — keeping it while narrowing can never win. Move it.
step 1 / 9
THE PATTERN — SO YOU RECOGNIZE IT NEXT TIME

Pointer as Proof of Elimination

YOU'LL SEE IT AGAIN WHEN

  • The answer is a function of a pair (i, j)
  • Moving one end gives a monotone argument that whole families of pairs can't win
  • One dimension shrinks, so something else must justify the move

SAME BLUEPRINT, DIFFERENT PROBLEM

Two Sum II (sorted)Trapping Rain Water3Sum
The bar isn't "solved it once." It's "could rebuild it from the observation."