Counting steps · routine
A pair in a sorted list
nums is sorted. Write has_pair(nums, target) returning True if two different positions hold numbers adding up to target.
Start one finger at each end. Too small? Move the left finger right. Too big? Move the right finger left. Each step rules out one number for good.
- right answers
- arguments left as they should be
- fast enough at scale
Run adds print(has_pair([1, 2, 4, 7], 9)) after your code, to try it.
Stuck on the idea rather than the code? The Two pointers and formulas lesson walks through it.