Two Pointers
Think of it like this
You need to find two friends in a lineup who together weigh exactly 100kg. The lineup is sorted lightest-to-heaviest. Instead of checking every possible pair (O(n²)), you put one finger at the lightest end and one at the heaviest. Their combined weight tells you which direction to move:
- Combined too light → move the left pointer right (get heavier)
- Combined too heavy → move the right pointer left (get lighter)
- Combined exactly 100 → found it!
This is the core idea: two pointers working together eliminate half the search space at each step.
The Three Variants
Variant 1, Opposite Ends (Inward)
Both pointers start at opposite ends and move toward each other. Works on sorted arrays.
