Two-Pointer Technique: Opposite-Direction & Fast-Slow Patterns0%

Two-Pointer Technique: Opposite-Direction & Fast-Slow Patterns

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Two-Pointer Technique: Opposite-Direction & Fast-Slow Patterns

The Two-Pointer Technique is an algorithmic pattern that uses two pointer indices to coordinate searches or mutations across an array or sequence. By moving pointers intelligently based on problem constraints, two-pointer algorithms reduce brute-force $O(n^2)$ quadratic nested loops into blazing $O(n)$ linear time with $O(1)$ auxiliary space.


1. Real-World Analogy: Folding a Ruler or Reading a Word

Imagine inspecting whether a word like "RADAR" is a palindrome:

  • You don't make a second copy of the word and check every combination.
  • Instead, you place your left index finger on the first letter 'R' and your right index finger on the last letter 'R'.
  • You verify they match, then slide your fingers inward simultaneously. If your fingers meet in the middle without any mismatches, it is a palindrome!

2. The Two Core Categories of Two Pointers

Category 1: Opposite-Direction Pointers (Converging)

  • Pointer left starts at index 0.
  • Pointer right starts at index n - 1.
  • Pointers advance towards each other (left++, right--) until they meet.
  • Top Applications: Reversing arrays, Palindrome checking, Two Sum in Sorted Array, Container with Most Water.

Category 2: Same-Direction Pointers (Fast & Slow)

  • Pointer slow tracks the boundary of clean/valid data.
  • Pointer fast scans forward through the array.
  • Top Applications: In-place element removal, deduplicating sorted arrays, moving zeros.

3. Classic Problem 1: Two Sum in a Sorted Array ($O(n)$)

Given a 1-indexed array of integers numbers sorted in non-decreasing order, find two numbers such that they add up to a specific target number:

Output
Array: [2, 7, 11, 15], Target = 18
Left = 0 (val 2), Right = 3 (val 15) -> Sum = 17 (< 18, so increment Left!)
Left = 1 (val 7), Right = 3 (val 15) -> Sum = 22 (> 18, so decrement Right!)
Left = 1 (val 7), Right = 2 (val 11) -> Sum = 18 (Target MATCH!)

Python 3 Implementation:

Python
def two_sum_sorted(numbers: list[int], target: int) -> list[int]:
left, right = 0, len(numbers) - 1
while left < right:
current_sum = numbers[left] + numbers[right]
if current_sum == target:
return [left + 1, right + 1] # 1-based index
elif current_sum < target:
left += 1 # Need a larger sum -> move left pointer right
else:
right -= 1 # Need a smaller sum -> move right pointer left
return []

Modern JavaScript / TypeScript Implementation:

TypeScript
export function twoSumSorted(numbers: number[], target: number): number[] {
let left = 0;
let right = numbers.length - 1;
 
while (left < right) {
const sum = numbers[left] + numbers[right];
if (sum === target) {
return [left + 1, right + 1];
} else if (sum < target) {
left++;
} else {
right--;
}
}
 
return [];
}

4. Classic Problem 2: Remove Duplicates from Sorted Array In-Place

Remove duplicates in-place such that each unique element appears only once, returning the count of unique items.

Python 3:

Python
def remove_duplicates(nums: list[int]) -> int:
if not nums:
return 0
write_idx = 1 # Slow pointer: position to write next unique element
for read_idx in range(1, len(nums)): # Fast pointer scans forward
if nums[read_idx] != nums[read_idx - 1]:
nums[write_idx] = nums[read_idx]
write_idx += 1
return write_idx

5. Trace Table: nums = [1, 1, 2, 2, 3]

read_idx (Fast)Current ValuePrevious ValueIs Unique?ActionModified Array
111NoSkip[1, 1, 2, 2, 3]
221Yesnums[1] = 2, write++[1, 2, 2, 2, 3]
322NoSkip[1, 2, 2, 2, 3]
432Yesnums[2] = 3, write++[1, 2, 3, 2, 3]

Result: First 3 elements are [1, 2, 3]. Return length 3 in $O(n)$ time with $O(1)$ space!


Interactive Quiz

Multiple Choice Questions

Question 1
What prerequisite condition is typically required to apply the opposite-direction Two-Pointer technique for the Two Sum problem?
Question 2
What is the time and space complexity of the Two-Pointer Two Sum solution on an already sorted array of size $n$?
Question 3
In the Fast & Slow pointer pattern for removing duplicates in-place, what does the "slow" pointer track?
Question 4
Which of the following classic LeetCode/interview problems is optimally solved using converging Two Pointers?
Question 5
Why is the Two-Pointer approach preferred over a nested loop for checking if a string is a palindrome?

Next Lesson

Sliding Window Algorithm: Fixed & Dynamic Window Strategies

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext