Math Fundamentals18 sections · 814 units
Open in Course

Container - Two Pointer Strategy

Greedy approach

Start with the widest container: left pointer at index 00, right pointer at index n1n - 1. This gives maximum width.

Compute the area. Then move the pointer pointing to the shorter line inward. Why? Moving the taller line inward can only decrease area (smaller width, same or smaller height).

But moving the shorter line might find a taller line that compensates for the width loss. This greedy strategy guarantees you find the maximum.