Loading section...

Opposite-End Convergence: The Core Pattern

Concepts: pyTwoPointers, pyConvergingPointers

This is the pattern you will use most often. Two pointers start at opposite ends of a sorted array and move toward each other. At each step, you compare the values at both pointers, make a decision (move left, move right, or return the answer), and eliminate a chunk of the search space. The pointers converge until they meet, at which point you have either found the answer or proven it does not exist. Two Sum on Sorted Input This is the canonical example, and it is the one you should be able to write from muscle memory. Given a sorted array and a target sum, find two numbers that add up to the target. The brute force is O(n^2): check every pair. Two pointers gives you O(n) with O(1) space. Walk through this with a concrete example. Array: [1, 3, 5, 7, 9], target: 8. Left starts at index 0 (