This document explains the Two Pointers Pattern, a fundamental algorithmic technique implemented across problems in the solution/ directory. The pattern uses two index variables that traverse an array or sequence in a coordinated manner to solve problems in linear time. This page focuses on the two main variants: same-direction pointers (fast-slow) and opposite-direction pointers (left-right).
For other array manipulation patterns, see Hash Table Pattern, Sliding Window Pattern, and Binary Search Pattern. For non-array algorithmic patterns, see DFS and Backtracking Pattern.
The two pointers technique optimizes array traversal by maintaining two index positions that move according to problem-specific rules. This eliminates the need for nested loops or additional data structures in many scenarios.
Sources: solution/0000-0099/0026.Remove Duplicates from Sorted Array/README.md1-96 solution/0000-0099/0027.Remove Element/README.md1-96 solution/0200-0299/0283.Move Zeroes/README.md1-68 solution/0000-0099/0080.Remove Duplicates from Sorted Array II/README.md1-99 solution/0000-0099/0011.Container With Most Water/README.md1-74 solution/0000-0099/0015.3Sum/README.md1-93
This variant uses two pointers that traverse the array in the same direction, typically from left to right. One pointer (k) tracks the position where the next valid element should be written, while the other pointer (i) scans through the array.
Sources: solution/0000-0099/0026.Remove Duplicates from Sorted Array/README.md76-85 solution/0000-0099/0027.Remove Element/README.md83-92
The general pattern follows this structure:
k = 0k, increment kk (length of processed array)Time Complexity: $O(n)$ - single pass through array
Space Complexity: $O(1)$ - in-place modification
Sources: solution/0000-0099/0026.Remove Duplicates from Sorted Array/README.md78-85
The simplest same-direction pattern removes all occurrences of a target value.
Algorithm:
k tracks count of non-target elementsx: if x != val, write x at position k and increment kk are ignored (not returned)Sources: solution/0000-0099/0027.Remove Element/README.md83-92 solution/0000-0099/0027.Remove Element/Solution.py1-9 solution/0000-0099/0027.Remove Element/Solution.java1-10
Moves all zeros to the end while maintaining relative order of non-zero elements.
Key Difference from Remove Element: Uses swap instead of simple assignment to preserve array elements.
Sources: solution/0200-0299/0283.Move Zeroes/README.md58-67 solution/0200-0299/0283.Move Zeroes/Solution.py1-8 solution/0200-0299/0283.Move Zeroes/Solution.java1-12
Removes duplicates from a sorted array, keeping only unique elements.
Key Condition: k == 0 or x != nums[k - 1]
k == 0)nums[k-1])Sources: solution/0000-0099/0026.Remove Duplicates from Sorted Array/README.md76-85 solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.py1-9
Extends Problem 26 to allow at most 2 (or K) occurrences of each element.
Condition Modified: k < 2 or x != nums[k - 2]
k < 2)k-2| Problem | Condition | Max Occurrences |
|---|---|---|
| Problem 26 | k == 0 or x != nums[k-1] | 1 |
| Problem 80 | k < 2 or x != nums[k-2] | 2 |
| General | k < K or x != nums[k-K] | K |
Sources: solution/0000-0099/0080.Remove Duplicates from Sorted Array II/README.md78-94 solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.py1-9
Sources: solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.cpp1-12 solution/0000-0099/0027.Remove Element/Solution.cpp1-11 solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.cpp1-12 solution/0200-0299/0283.Move Zeroes/Solution.cpp1-12
This variant uses two pointers starting at opposite ends of the array, moving toward each other based on problem conditions. Typically used on sorted arrays to find pairs, triplets, or optimize area/sum calculations.
Sources: solution/0000-0099/0011.Container With Most Water/README.md62-72 solution/0000-0099/0015.3Sum/README.md73-91
l = 0 (left), r = n - 1 (right)l < r: nums[l] and nums[r]l++r--Time Complexity: $O(n)$ for single pass
Space Complexity: $O(1)$
Sources: solution/0000-0099/0011.Container With Most Water/README.md64-72
Finds two lines that form a container with maximum water capacity.
Key Insight: Always move the pointer with the shorter height, as moving the taller one cannot improve the result (width decreases and height is still limited by the shorter line).
Sources: solution/0000-0099/0011.Container With Most Water/README.md62-72 solution/0000-0099/0011.Container With Most Water/Solution.py1-13
Finds all unique triplets that sum to zero. Combines sorting with opposite-direction two pointers.
Algorithm Structure:
nums.sort()nums[i] (index i from 0 to n-3)nums[j] + nums[k] = -nums[i]Duplicate Handling:
nums[i] if same as nums[i-1] (line check in outer loop)j and k values in inner loopTime Complexity: $O(n^2)$ - $O(n \log n)$ for sorting + $O(n)$ for outer loop × $O(n)$ for two pointers
Space Complexity: $O(\log n)$ for sorting (depends on sort implementation)
Sources: solution/0000-0099/0015.3Sum/README.md73-93 solution/0000-0099/0015.3Sum/Solution.py1-26 solution/0000-0099/0015.3Sum/Solution.java1-29
| Characteristics | Example Problems |
|---|---|
| In-place array modification required | Problem 26, 27, 80, 283 |
| Maintain relative order | Problem 283 (Move Zeroes) |
| Process array elements based on condition | Problem 27 (Remove Element) |
| Remove duplicates from sorted array | Problem 26, 80 |
| Linear time, constant space requirement | All same-direction problems |
| Characteristics | Example Problems |
|---|---|
| Sorted array (or can be sorted) | Problem 15 (3Sum) |
| Find pairs/triplets with target sum | Problem 15 |
| Optimize area/volume calculations | Problem 11 (Container) |
| Compare values from both ends | Problem 11 |
| Need to explore all combinations efficiently | Problem 15 |
Sources: solution/0000-0099/0026.Remove Duplicates from Sorted Array/README_EN.md76-97 solution/0000-0099/0015.3Sum/README_EN.md69-89
All two pointers solutions maintain consistent logic across 10+ programming languages. The core algorithm remains identical, with only syntax differences.
Python: Uses enumerate for index+value, list comprehensions
Java/C++: Enhanced for-loop or traditional index loop
Go: Range iteration with multiple return values
Rust: Explicit bounds checking and ownership
Sources: solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.py1-9 solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.java1-10 solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.go1-10 solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.rs1-13
A specialized two-pointer variant (candidate and counter) for finding majority element.
Pattern: Single pass with candidate tracking
cnt acts as a balance between candidate matches and non-matchescnt == 0, select new candidateSources: solution/0100-0199/0169.Majority Element/README.md60-73 solution/0100-0199/0169.Majority Element/README_EN.md53-65
For problems requiring more than two pointers:
| Pattern | Time Complexity | Space Complexity | Problem Examples |
|---|---|---|---|
| Same-Direction | $O(n)$ | $O(1)$ | 26, 27, 80, 283 |
| Opposite-Direction | $O(n)$ | $O(1)$ | 11 |
| With Sorting | $O(n^2)$ or $O(n \log n)$ | $O(\log n)$ | 15 |
Sources: solution/0000-0099/0026.Remove Duplicates from Sorted Array/README.md85 solution/0000-0099/0015.3Sum/README.md93 solution/0000-0099/0011.Container With Most Water/README.md72
Refresh this wiki
This wiki was recently refreshed. Please wait 4 days to refresh again.