Skip to content

Two Pointer Technique

The Two Pointer technique uses two pointers (index variables) to traverse an array, typically moving toward each other or in the same direction at different speeds. It reduces time complexity from O(n²) to O(n) in many cases.

Two Pointer Visualization

Use Cases: Reverse array, palindrome check, two-sum in sorted array

/**
* Reverse array in-place
* Left pointer starts at 0, right pointer at end.
* Swap, then move L→, R← until they meet.
*/
function reverseArray(arr) {
let left = 0;
let right = arr.length - 1;
while (left < right) {
// Swap
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right--;
}
return arr;
}
console.log(reverseArray([1, 2, 3, 4, 5])); // [5, 4, 3, 2, 1]
/**
* Check if a string is a palindrome
* Compare characters at left and right, moving inward.
*/
function isPalindrome(str) {
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) return false;
left++;
right--;
}
return true;
}
console.log(isPalindrome("racecar")); // true
console.log(isPalindrome("hello")); // false
/**
* Two Sum in SORTED array
* Since array is sorted, if sum > target, move right pointer left.
* If sum < target, move left pointer right.
*/
function twoSumSorted(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const sum = nums[left] + nums[right];
if (sum === target) return [left, right];
if (sum < target) left++;
else right--;
}
return [-1, -1];
}
console.log(twoSumSorted([2, 7, 11, 15], 9)); // [0, 1]

Use Cases: Cycle detection, middle of linked list

/**
* Find middle of an array
* Fast pointer moves 2 steps, slow moves 1 step.
* When fast reaches end, slow is at the middle.
*/
function findMiddle(arr) {
let slow = 0;
let fast = 0;
while (fast < arr.length && fast + 1 < arr.length) {
slow++;
fast += 2;
}
return arr[slow];
}
console.log(findMiddle([1, 2, 3, 4, 5])); // 3
console.log(findMiddle([1, 2, 3, 4, 5, 6])); // 4

3. Same Direction Pointers (Sliding Window Variation)

Section titled “3. Same Direction Pointers (Sliding Window Variation)”

Use Cases: Removing duplicates from sorted array, in-place array operations

/**
* Remove duplicates from sorted array in-place
* Slow pointer tracks unique elements position.
* Fast pointer scans ahead for new values.
*/
function removeDuplicates(nums) {
if (nums.length === 0) return 0;
let slow = 0;
for (let fast = 1; fast < nums.length; fast++) {
if (nums[fast] !== nums[slow]) {
slow++;
nums[slow] = nums[fast];
}
}
return slow + 1; // length of unique portion
}
console.log(removeDuplicates([1, 1, 2, 2, 3, 4, 4, 5]));
// Returns 5, array becomes [1, 2, 3, 4, 5, ...]
SignalPatternExample Problems
Sorted arrayPointers toward each otherTwo Sum II, Three Sum
PalindromeStart and end compareValid Palindrome
In-place reverseSwap endsReverse array/string
Remove duplicatesSlow/fast same directionRemove Duplicates
Find middleFast/slowMiddle of Linked List
Container areaMaximize areaContainer With Most Water
Triplet sumFix one, two-point restThree Sum
ProblemBrute ForceTwo Pointer
Two Sum (sorted)O(n²)O(n)
Three SumO(n³)O(n²)
Palindrome CheckO(n²)O(n)
Remove DuplicatesO(n²)O(n)
Reverse ArrayO(n)O(n) — but O(1) extra space

Next: Sliding Window →