What two pointers are
After this: Say what a pointer is here, and find a small pair by moving two pointers.
On this page · 5 sections · 3 checks
Imagine a shop shelf with four items, sorted from cheapest to most expensive. Their prices are 1, 3, 5 and 8. You have a gift card worth exactly 11, and you want to buy two items that use all of it. Which two do you pick?
You can probably see the answer: 3 and 8. A computer cannot see it. It has to work through the numbers in some order. In a program, the prices are an array, [1, 3, 5, 8], and the question is: do two of these numbers add up to 11?
This tutorial teaches a fast way to answer questions like this. You keep track of two positions in the array, and you move them by a small rule. The method is called two pointers.
What a pointer is
Each cell of an array has an index, which is its position number, counting from 0. In this tutorial, a is a variable that holds an index. It is an ordinary integer variable, and nothing more.
Here are two pointers, called left and right.
nums = [1, 3, 5, 8]left = 0right = 3print(nums[left], nums[right])left += 1 # move left one cell to the rightprint(nums[left], nums[right])1 8 3 8
int[] nums = {1, 3, 5, 8};int left = 0;int right = 3;System.out.println(nums[left] + " " + nums[right]);left++; // move left one cell to the rightSystem.out.println(nums[left] + " " + nums[right]);1 8 3 8
left holds 0, so nums[left] is the first number, 1. right holds 3, so nums[right] is the last number, 8.
Adding 1 to left is what we mean by "moving the pointer" one cell to the right. Now nums[left] is 3. No value in the array moved or changed. Only the variable changed.
You can picture a pointer as a finger resting on one cell. The visualizer shows each pointer as a small label under its cell.
The smallest example
Now back to the gift card. Put one pointer on each end of [1, 3, 5, 8], and add the two numbers.
leftis on 1 andrightis on 8. 1 + 8 = 9, which is less than 11.rightis already on the largest number, so the only way to get a bigger sum is to moveleftone cell to the right.leftis on 3 andrightis on 8. 3 + 8 = 11. Found.
That took two tries. A computer that tried the pairs in order would need five: 1 + 3, 1 + 5, 1 + 8, 3 + 5 and then 3 + 8.
So this is why anyone wants two pointers: they save work. Here the saving is small, but it becomes very large on long arrays. The next two lessons show how large, and why the method can never miss the answer.
Watch it happen
Run the same search here. The labels left and right are the two pointers.
- Two pointers: sum 11The target is 11. Watch left and right, the two labels under the cells.
- 01
- 13
- 25
- 38
Run an operation to begin.
Check yourself
In this tutorial, what is a pointer?
What does this print?
nums = [2, 4, 6, 9]left = 0right = 3left += 1right -= 1print(nums[left], nums[right])int[] nums = {2, 4, 6, 9};int left = 0;int right = 3;left++;right--;System.out.println(nums[left] + " " + nums[right]);The sorted array is [2, 3, 6, 9] and the target is 12. left is on 2 and right is on 9, so the sum is 11. What should happen next?
What comes next
There is nothing new to build in this tutorial. The array stays a plain array, and each lesson gives you a different rule for moving two pointers.
- Trying every pair: the slow way, and how to measure it.
- From both ends: the pointers start at the two ends and move towards each other.
- Fast and slow: both pointers move the same way, at different speeds.
- Windows: the pointers mark the two ends of a stretch of the array.
- Strings: the same rules, used on text.
- Say what a pointer is in this tutorial: an ordinary integer variable that holds an index.
- Move a pointer by adding or subtracting 1, and read the cell it is on.
- Say why the method is useful: it finds a pair in fewer tries than checking every pair.