Two Pointers

Use two indices moving toward each other or in the same direction to solve pair and partition problems.

8 Problems2-3 hours3 Easy4 Medium1 Hard

Overview

The two-pointer technique uses two index variables that traverse the array — usually one from each end (converging) or both from the start (fast/slow). It reduces O(n²) brute-force pair searches to O(n) by avoiding redundant comparisons.

Complexity Summary

Time Complexity

O(n) — each pointer moves at most n steps

Space Complexity

O(1) — no extra space needed

Key Patterns & Techniques

Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.

1

Converging Pointers

Concept

Use this when the array is sorted or when the answer depends on comparing values from both ends

Why It Works

Because the input is ordered, moving one pointer changes the result in a predictable direction. This lets you eliminate many impossible pairs without checking them explicitly.

Pattern Example
Problem

Problem Two Sum II. Start with one pointer at the beginning and one at the end, compare their sum with the target, and move the pointer that makes the sum closer.

Implementation
// Converging pointers — Two Sum II (sorted array)
function twoSumSorted(nums, target) {
  let left = 0, right = nums.length - 1;
  while (left < right) {
    const sum = nums[left] + nums[right];
    if (sum === target) return [left + 1, right + 1];
    if (sum < target) left++;
    else right--;
  }
  return [];
}
Practice questions for this pattern
2

Fast and Slow Pointers

Concept

Use this when two traversals moving at different speeds reveal structure such as a middle node, a cycle, or a repeating state

Why It Works

The slow pointer gives position, while the fast pointer helps detect relative motion. This pattern is especially common in linked lists but also appears in arrays and strings.

Pattern Example
Problem

Problem Linked List Cycle. Move slow by one step and fast by two steps; if they ever meet, a cycle exists.

Implementation
// Fast/slow pointers — Linked List Cycle detection
function hasCycle(head) {
  let slow = head, fast = head;
  while (fast && fast.next) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow === fast) return true;
  }
  return false;
}
Practice questions for this pattern
3

Read/Write Partitioning

Concept

Use this when you need to rearrange elements in-place while preserving a region of already processed values

Why It Works

One pointer reads the current element, and another pointer marks where the next kept element should be written. This avoids extra arrays and makes in-place partition problems very manageable.

Pattern Example
Problem

Problem Move Zeroes. Scan with the fast pointer, and each time you see a non-zero value, swap it into the next write position.

Implementation
// Read/write partition — Move Zeroes in-place
function moveZeroes(nums) {
  let slow = 0;
  for (let fast = 0; fast < nums.length; fast++) {
    if (nums[fast] !== 0) {
      [nums[slow], nums[fast]] = [nums[fast], nums[slow]];
      slow++;
    }
  }
}
Practice questions for this pattern
4

Duplicate Compression

Concept

Use this when a sorted array contains repeated values and you want to keep only one copy or a limited number of copies

Why It Works

The sorted order ensures equal values are adjacent, which makes comparison against the last kept value enough. This is a classic in-place array pattern.

Pattern Example
Problem

Problem Remove Duplicates from Sorted Array. Keep a write pointer for the last unique value and copy forward only when a new unique value appears.

Implementation
// Duplicate compression — Remove Duplicates from Sorted Array
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]) nums[++slow] = nums[fast];
  }
  return slow + 1;
}
Practice questions for this pattern
5

Sorted Input Recognition

Concept

Use this as a decision rule before applying two pointers

Why It Works

If the array is already sorted, two pointers often works directly. If it is not sorted but order does not matter, sorting first may unlock a cleaner solution. This mindset helps you recognize when a brute-force pair or triplet search can be improved.

Pattern Example
Problem

Problem 3Sum. Sort the array first, then fix one value and run a two-pointer search on the remaining suffix.

Implementation
// Sorted input recognition — 3Sum with sort + two pointers
function threeSum(nums) {
  nums.sort((a, b) => a - b);
  const result = [];
  for (let i = 0; i < nums.length - 2; i++) {
    if (i > 0 && nums[i] === nums[i-1]) continue;
    let left = i + 1, right = nums.length - 1;
    while (left < right) {
      const sum = nums[i] + nums[left] + nums[right];
      if (sum === 0) {
        result.push([nums[i], nums[left], nums[right]]);
        while (left < right && nums[left] === nums[left+1]) left++;
        while (left < right && nums[right] === nums[right-1]) right--;
        left++; right--;
      } else if (sum < 0) left++;
      else right--;
    }
  }
  return result;
}
Practice questions for this pattern
6

Fix One, Search the Rest

Concept

Use this when the problem involves triplets or more complex combinations but still has an ordered structure

Why It Works

Fix one element, reduce the remaining work to a simpler two-pointer problem, and repeat. This is a very common interview reduction technique because it turns O(n³) brute force into O(n²).

Pattern Example
Problem

Problem 3Sum. Fix index i, then use left and right pointers on the subarray after i to find pairs that complement nums[i].

Implementation
// Fix one, search the rest — Container With Most Water
function maxArea(height) {
  let left = 0, right = height.length - 1, best = 0;
  while (left < right) {
    const area = (right - left) * Math.min(height[left], height[right]);
    best = Math.max(best, area);
    if (height[left] < height[right]) left++;
    else right--;
  }
  return best;
}
Practice questions for this pattern
DSA Practice Online - 150+ Coding Interview Questions | LeetCode Alternative | InstaMock - AI Mock Interview