The Two Pointer technique uses two pointers to scan through data efficiently. It often turns O(n²) brute force into O(n)! 👆👆
💡 Think of it like this: Imagine two runners on a track. They can start from opposite ends and walk toward each other, or one can walk ahead while the other follows. By coordinating their movements, they can solve problems much faster than if one person checked every spot.
Three common patterns:
When to use two pointers: Sorted arrays, palindrome checks, partitioning, finding pairs that sum to a target. The key insight is that sorted data lets us make smart decisions about which pointer to move!
has_pair_sum([1, 2, 3, 4, 6], 6)
True
has_pair_sum([1, 2, 3, 4, 6], 12)
False