Lens
Algorithms you can watch. Press play, step through, try your own numbers, in Python, JavaScript, Java or C++.
- Arrays & HashingTwo numbers that add up to a target, any orderInstead of checking every pair, remember what you've seen. The partner you need is one look-up away.O(n) time · O(n) space
- Two PointersTwo numbers that add up to a targetThe list is sorted, so a small sum can only grow by moving left up, and a big sum can only shrink by moving right down.O(n) time · O(1) space
- Sliding WindowMaximum sum of k numbers in a rowDon't re-add k numbers every time. Add the one that enters, subtract the one that leaves.O(n) time · O(1) space
- Binary SearchFind a number in a sorted listOne look at the middle rules out half the list. 1,000,000 numbers take at most 20 looks.O(log n) time · O(1) space
- Kadane's AlgorithmBiggest sum of numbers in a rowA run with a negative sum can only make what comes next smaller. Drop it and start fresh.O(n) time · O(1) space