Two Pointers

O(n)

Use two pointers to traverse an array or string, reducing time complexity from O(n^2) to O(n) for problems involving pairs or subarrays.

Time: O(n)Space: O(1)

When to Use

  • Finding pairs that satisfy a condition (sum, product)
  • Removing duplicates from sorted array
  • Reversing or partitioning arrays in-place
  • Detecting cycles in linked lists (slow/fast)

Pattern Variants

Converging Pointers

Start pointers at opposite ends, move toward each other based on comparison.

Use for: Sorted arrays, pair sum problems, palindrome checks

Same Direction (Slow/Fast)

Both pointers start at beginning, fast pointer moves ahead to find patterns.

Use for: Remove duplicates, cycle detection, find middle element

Partition (Dutch Flag)

Three-way partition using two pointers to separate elements into regions.

Use for: Sort colors (0s, 1s, 2s), partition around pivot

Interactive Visualization

Code
1function twoSum(nums, target) {
2 let left = 0
3 let right = nums.length - 1
4
5 while (left < right) {
6 const sum = nums[left] + nums[right]
7
8 if (sum === target) {
9 return [left, right]
10 } else if (sum < target) {
11 left++
12 } else {
13 right--
14 }
15 }
16 return []
17}
10
31
42
53
74
105
116
Step 1/17We need to find two numbers in this SORTED array that add up to 9. Two pointers let us search efficiently.
1 / 17
Key Insight:Two pointers on sorted array avoids O(n^2) nested loops - we eliminate candidates from both ends simultaneously.

Practice this Pattern

Go to Two Sum II (Sorted)

Two Sum II (Sorted)

Find two numbers in a sorted array that add up to target

Go to Valid Palindrome

Valid Palindrome

Check if string is palindrome (ignoring non-alphanumeric)

Go to Reverse String

Reverse String

Reverse array of characters in-place using two pointers

Go to Remove Duplicates (Sorted)

Remove Duplicates (Sorted)

Remove duplicates from sorted array in-place, return new length

Go to Move Zeroes

Move Zeroes

Move all zeroes to end while maintaining order of non-zero elements

Go to Squares of Sorted Array

Squares of Sorted Array

Return squares of sorted array in sorted order (handles negatives)

Go to Container With Most Water

Container With Most Water

Find two lines that form container holding most water

Go to 3Sum

3Sum

Find all unique triplets that sum to zero

Go to Sort Colors (Dutch Flag)

Sort Colors (Dutch Flag)

Sort array of 0s, 1s, 2s in-place using three pointers

Go to Remove Element

Remove Element

Remove all instances of value in-place, return new length

Go to Is Subsequence

Is Subsequence

Check if s is a subsequence of t

Go to Merge Sorted Array

Merge Sorted Array

Merge two sorted arrays into first array in-place

Go to Partition Labels

Partition Labels

Partition string so each letter appears in at most one part

Go to Trapping Rain Water

Trapping Rain Water

Calculate how much rain water can be trapped between bars

Go to 3Sum Closest

3Sum Closest

Find three integers whose sum is closest to target

Go to Valid Palindrome II

Valid Palindrome II

Check if string can be a palindrome by removing at most one character

Go to Boats to Save People

Boats to Save People

Minimum boats to carry people with weight limit

Go to Rotate Array

Rotate Array

Rotate array to the right by k steps using triple reverse

Go to 4Sum

4Sum

Find all unique quadruplets that sum to target

Go to Long Pressed Name

Long Pressed Name

Check if typed string could be result of long pressing name

Go to Intersection of Two Arrays II

Intersection of Two Arrays II

Find intersection of two arrays including duplicates

Go to Remove Duplicates from Sorted Array II

Remove Duplicates from Sorted Array II

Remove duplicates allowing at most two of each element

Go to Backspace String Compare

Backspace String Compare

Compare two strings with backspace characters (#)

Go to String Compression

String Compression

Compress string in-place using read/write pointers

Go to Two Sum

Two Sum

Find two numbers that add up to target using hash map