The Two Pointers pattern uses two converging or fast/slow pointer indices to search pairs or process arrays in linear O(N) time.
Two friends walk toward each other from opposite ends of a street until they meet at the target location.
500 steps (Linear)
O(N) Linear Time
O(1) / O(N) Space
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Two Pointers Loop | O(N) | O(1) |
function twoSumSorted(arr, target) {
let left = 0, right = arr.length - 1;
while (left < right) {
const sum = arr[left] + arr[right];
if (sum === target) return [left, right];
if (sum < target) left++;
else right--;
}
return [];
}Compares start and end string characters moving inward.
Swaps left and right pointer elements.
Validate your conceptual understanding, operation mechanics, and core concepts of Two Pointers.
Two Pointers requires a sorted array when searching for target sums to determine whether to advance left or decrement right!
Array loaded. Set target sum and click Find Pair to watch left/right pointers converge.