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.
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
1function twoSum(nums, target) {2 let left = 03 let right = nums.length - 145 while (left < right) {6 const sum = nums[left] + nums[right]78 if (sum === target) {9 return [left, right]10 } else if (sum < target) {11 left++12 } else {13 right--14 }15 }16 return []17}
Practice this Pattern
Two Sum II (Sorted)
Find two numbers in a sorted array that add up to target
Valid Palindrome
Check if string is palindrome (ignoring non-alphanumeric)
Reverse String
Reverse array of characters in-place using two pointers
Remove Duplicates (Sorted)
Remove duplicates from sorted array in-place, return new length
Move Zeroes
Move all zeroes to end while maintaining order of non-zero elements
Squares of Sorted Array
Return squares of sorted array in sorted order (handles negatives)
Container With Most Water
Find two lines that form container holding most water
3Sum
Find all unique triplets that sum to zero
Sort Colors (Dutch Flag)
Sort array of 0s, 1s, 2s in-place using three pointers
Remove Element
Remove all instances of value in-place, return new length
Is Subsequence
Check if s is a subsequence of t
Merge Sorted Array
Merge two sorted arrays into first array in-place
Partition Labels
Partition string so each letter appears in at most one part
Trapping Rain Water
Calculate how much rain water can be trapped between bars
3Sum Closest
Find three integers whose sum is closest to target
Valid Palindrome II
Check if string can be a palindrome by removing at most one character
Boats to Save People
Minimum boats to carry people with weight limit
Rotate Array
Rotate array to the right by k steps using triple reverse
4Sum
Find all unique quadruplets that sum to target
Long Pressed Name
Check if typed string could be result of long pressing name
Intersection of Two Arrays II
Find intersection of two arrays including duplicates
Remove Duplicates from Sorted Array II
Remove duplicates allowing at most two of each element
Backspace String Compare
Compare two strings with backspace characters (#)
String Compression
Compress string in-place using read/write pointers
Two Sum
Find two numbers that add up to target using hash map