Search
⌘K
Get Premium
Two Pointers

Two-Pointer Overview


This technique refers to using two pointers that start at opposite ends of an array and gradually move towards each other.
134681013leftright
In this page, we'll cover:
  1. A simple problem that illustrates the motivation behind the two-pointer technique.
  2. The types of problem for which you should consider using this technique.
  3. A list of problems (with animated solutions!) for you to try that build upon the concepts covered here.

Problem: Two Sum

DESCRIPTION
Given a sorted array of integers nums, determine if there exists a pair of numbers that sum to a given target.
Example: Input: nums = [1,3,4,6,8,10,13], target = 13 Output: True (3 + 10 = 13)
Input: nums = [1,3,4,6,8,10,13], target = 6 Output: False
The naive approach to this problem uses two-pointers i and j in a nested for-loop to consider each pair in the input array, for a total of O(n2) pairs considered.
|
comma-separated integers
|
integer
Try these examples:
Visualization
def isPairSum(nums, target):
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return True
return False
134681013

two sum naive

0 / 11

However, if we put a bit more thought into how we initialize our pointers and how we move them, we can eliminate the number of pairs we consider down to O(n). Understanding why we are able to eliminate pairs is key to understanding the two-pointer technique.
134681013ij
134681013ij
134681013ij
134681013ij
134681013ij
134681013ij
134681013ij
134681013ij
134681013ij
134681013ij13
134681013leftright
134681013leftright
134681013leftright13
Naive (left) vs. Two-Pointer Technique (right)

Eliminating Pairs

The two-pointer technique leverages the fact that the input array is sorted.
Let's use it to solve the Two Sum problem when nums = [1, 3, 4, 6, 8, 10, 13] and target = 13.
134681013
Goal: find this pair of numbers that sum to 13
We start by initializing two pointers at opposite ends of the array, which represent the pair of numbers we are currently considering.
134681013
This pair has a sum (14) that is greater than our target (13). And because our array is sorted, all other pairs ending at our right pointer (13) also have sums greater than our target, as they all involve numbers greater than 1, the value at our left pointer.
134681013leftright
So, to move onto the next pair we move our right pointer back, which elimininates those unnecessary pairs from our search.
134681013leftright1617192123
Move right pointer back.
Now, since our sum is less than our target, we know that all other pairs involving our left pointer also have sums less than our target. So, we move our left pointer forward to eliminate those unnecessary pairs and arrive at the next pair to consider.
134681013leftright
This continues until either our pointers meet (in which case we did not find a successful pair) or until we find a pair that sums to our target, like we did here.
134681013leftright

Solution

|
sorted, comma-separated integers
|
integer
Try these examples:
Visualization
def twoSum(nums, target):
left, right = 0, len(nums) - 1
while left < right:
current_sum = nums[left] + nums[right]
if current_sum == target:
return True
if current_sum < target:
left += 1
else:
right -= 1
return False
134681013

two sum algorithm

0 / 7

Summary

  • The two-pointer technique leverages the fact that the input array is sorted to eliminate the number of pairs we consider from O(n2)down to O(n).
  • The two-pointers start at opposite ends of the array, and represent the pair of numbers we are currently considering.
  • We repeatedly compare the sum of the current pair to the target, and move a pointer in a way that eliminates unnecessary pairs from our search.

When Do I Use This?

Consider using the two-pointer technique for questions that involve searching for a pair (or more) of items in an array that meet a certain criteria.
Examples:
  • Finding a pair of items that sum to a given target in an array.
  • Finding a triplet of items that sum to 0 in a given array.
  • Finding the maximum amount of water that can be held between two array items representing wall heights.

Practice Problems

Try applying the concepts related to eliminating unnecessary pairs to the following problems:
Medium
Medium
Medium

Bonus: Additional Problems

These problems also use two pointers in an array, but instead, each pointer represents a logical "region" of the array.
Easy
Medium
Hard

Your account is free and you can post anonymously if you choose.