Codele / Algorithms
Two pointers
Two cards from a sorted row must add up to a target. Instead of trying every pair, put one finger at each end and move them towards each other.
- Two pointers means two markers (fingers) moving through the same row, here from both ends.
- It needs a sorted row. That is how we know which finger to move.
- Sum too small: move the left finger right. Sum too big: move the right finger left.
- Every look rules out one card for good, so a row of n cards needs fewer than n looks, not every pair.
- If the fingers meet, no pair adds up to the target.
Loading the playable lesson…