Two Sum II (LeetCode 167): two pointers on a sorted array
Two Sum II (LeetCode 167): a pointer at each end of the sorted array, too heavy moves right one left, too light moves left one right. O(n), O(1).
Two Sum II (LeetCode 167) gives you a list of numbers sorted from smallest to largest and a target, and asks for the two numbers that add up to the target. You return their positions counted from one, not from zero, and the problem promises there is exactly one answer.
Our example is [1, 3, 4, 6, 8, 11] with target 10. You could try every pair, but with n numbers that is about n²/2 sums. Because the list is sorted, two pointers find the pair in one pass.
Where do the two pointers start?
Put i on the first number, index 0, and j on the last, index len(numbers) - 1, which is 5 here. So the first pair is the smallest number with the largest one.

Picture a seesaw: the sum of the pair sits on one side and the target on the other. Every pass of the loop adds the two numbers and looks at which way it tips.
Which pointer moves, and why?
There are three outcomes:
- the sum equals the target: return
[i + 1, j + 1], the positions counted from one; - the sum is too heavy (bigger than the target): move
jone step left, to a smaller number; - the sum is too light (smaller than the target): move
ione step right, to a bigger number.

The sorted order is what makes this safe. If the sum is too heavy, numbers[i] is the smallest number still in play, so numbers[j] is too big even with the smallest partner it has left. It can't be part of the answer, and dropping it loses nothing. In the same way, when the sum is too light, numbers[i] is too small even with the biggest partner left, so it goes. Each step drops one number for good.
How does the walk go for target 10?

| Step | i | j | Pair | Sum | Verdict | Move |
|---|---|---|---|---|---|---|
| 1 | 0 | 5 | 1 + 11 | 12 | too heavy | j to 4 |
| 2 | 0 | 4 | 1 + 8 | 9 | too light | i to 1 |
| 3 | 1 | 4 | 3 + 8 | 11 | too heavy | j to 3 |
| 4 | 1 | 3 | 3 + 6 | 9 | too light | i to 2 |
| 5 | 2 | 3 | 4 + 6 | 10 | balanced | return [3, 4] |
The pointers take turns here, but they don't have to: each step simply moves whichever side can bring the sum closer to the target.

At step 5 the 4 at index 2 and the 6 at index 3 make exactly 10. Adding one to each index gives the positions LeetCode asks for, 3 and 4.
![4 + 6 = 10 balances the seesaw: the 4 and the 6 turn teal and the function returns [3, 4], with return [i + 1, j + 1] highlighted](https://vthvqgqmgbnywvxzqlws.supabase.co/storage/v1/object/public/blog-media/shorts/alg-007-two-sum-sorted/balanced.jpg)
What does the program look like?
This is the program from the video, line for line:
def two_sum(numbers, target):
i, j = 0, len(numbers) - 1
while i < j:
s = numbers[i] + numbers[j]
if s == target:
return [i + 1, j + 1]
elif s > target:
j -= 1
else:
i += 1
print(two_sum([1, 3, 4, 6, 8, 11], 10))
Output:
[3, 4]
The loop never needs a "not found" branch, because LeetCode 167 guarantees exactly one solution.
Why is it O(n) time and O(1) space?
Every pass either returns or moves one pointer one step toward the other, and they never move back. They can take at most n - 1 steps before they meet, so the work grows linearly: O(n). The only extra memory is i, j and the sum s, whatever the length of the list: O(1). The hash-map solution to the original Two Sum is also O(n) time, but it needs O(n) extra space; the sorted order is what lets two pointers skip the dictionary.
Try it
Keep the same list, [1, 3, 4, 6, 8, 11], and change the target to 11. Walk the pointers in your head: which two numbers balance the seesaw, and which positions does the function return?