Two pointers
Lesson 4 of 20 · Algo.Micro
Find a pair that adds up to a target in one pass, instead of checking every pair.
Check yourself
Which two numbers add up to 52?
Trying every pair works, but with n numbers there are about n²/2 pairs. For 10,000 numbers that is 50 million sums.
Check yourself
Sum is smaller than the target. Which pointer moves?
Do it in the browser: a real algorithms tool, a short mission and instant feedback. No install.
Start this lesson — free