Skip to content

Understanding the Two Pointer Pattern in DSA

While solving array problems in Data Structures and Algorithms (DSA), I discovered a powerful technique called the Two Pointer Pattern. It helps reduce unnecessary work and often turns slow solutions into efficient ones.

In this blog, I’ll explain what the Two Pointer Pattern is, when to use it, and how it works with simple examples.


What is the Two Pointer Pattern?

The Two Pointer Pattern is a technique where we use two indices (pointers) to traverse an array or string instead of using just one.

For example:

int left = 0;
int right = arr.length - 1;

Here:

  • left starts from the beginning.
  • right starts from the end.

The pointers move according to the problem’s requirements.


Why Do We Need It?

Consider finding two numbers in a sorted array whose sum equals a target value.

Brute Force Approach

for(int i = 0; i < n; i++) {
    for(int j = i + 1; j < n; j++) {
        if(arr[i] + arr[j] == target) {
            return true;
        }
    }
}

Time Complexity

O(n²)

This works, but it becomes slow for large arrays.

The Two Pointer Pattern can often solve the same problem in:

O(n)

which is much faster.


Basic Idea

Imagine the following sorted array:

1 2 3 4 5 6 7 8 9

Place two pointers:

L               R
1 2 3 4 5 6 7 8 9

Check the sum:

1 + 9 = 10

Depending on the result:

  • If the sum is too small, move the left pointer.
  • If the sum is too large, move the right pointer.
  • If the sum matches the target, we found the answer.

This allows us to eliminate many possibilities at once.


Types of Two Pointer Problems

1. Opposite Direction Pointers

Pointers start at opposite ends and move toward each other.

L             R
1 2 3 4 5 6 7 8

Common problems:

  • Two Sum (Sorted Array)
  • Palindrome Checking
  • Container With Most Water
  • Trapping Rain Water

2. Same Direction Pointers

Both pointers move forward.

L R
1 2 3 4 5

Common problems:

  • Remove Duplicates
  • Move Zeroes
  • Array Partitioning

3. Fast and Slow Pointers

One pointer moves faster than the other.

slow -> 1 step
fast -> 2 steps

Common problems:

  • Linked List Cycle Detection
  • Finding the Middle Node

Example 1: Two Sum in a Sorted Array

Problem

Find two numbers whose sum equals 6.

[1, 2, 3, 4, 6]

Solution

int left = 0;
int right = arr.length - 1;

while(left < right) {

    int sum = arr[left] + arr[right];

    if(sum == target) {
        return true;
    }
    else if(sum < target) {
        left++;
    }
    else {
        right--;
    }
}

Complexity

  • Time: O(n)
  • Space: O(1)

Example 2: Palindrome Checking

A palindrome reads the same forward and backward.

Example:

madam

Idea

Compare characters from both ends.

m a d a m
↑       ↑

Move inward after each successful comparison.

Code

int left = 0;
int right = s.length() - 1;

while(left < right) {

    if(s.charAt(left) != s.charAt(right)) {
        return false;
    }

    left++;
    right--;
}

return true;

Complexity

  • Time: O(n)
  • Space: O(1)

Example 3: Remove Duplicates

Given:

1 1 2 2 3

Expected result:

1 2 3

Idea

Use:

  • Slow pointer → position to write.
  • Fast pointer → position to read.

Code

int slow = 0;

for(int fast = 1; fast < arr.length; fast++) {

    if(arr[fast] != arr[slow]) {
        slow++;
        arr[slow] = arr[fast];
    }
}

Complexity

  • Time: O(n)
  • Space: O(1)

How to Identify Two Pointer Problems

Look for these clues:

1. The input is sorted

A sorted array is often a strong hint.

2. Finding pairs or triplets

Examples:

  • Two Sum
  • Three Sum
  • Pair Difference

3. Comparing from both ends

Examples:

  • Palindrome
  • Reverse String

4. Constant space requirement

Many Two Pointer solutions use only a few variables.

5. Subarray or substring questions

This often leads to Sliding Window, which is a specialized form of the Two Pointer Pattern.


Common Mistakes

Forgetting to Move a Pointer

while(left < right) {
    // no pointer movement
}

This causes an infinite loop.


Moving the Wrong Pointer

Always ask:

  • Will moving this pointer increase the result?
  • Will moving this pointer decrease the result?

Understanding this logic is crucial.


Using Two Pointers on Unsorted Data

Many Two Pointer techniques require the array to be sorted first.

Always verify whether sorting is necessary.


Learning Roadmap

Beginner

  1. Reverse String
  2. Palindrome Check
  3. Two Sum II
  4. Valid Palindrome

Intermediate

  1. Remove Duplicates from Sorted Array
  2. Move Zeroes
  3. Squares of a Sorted Array
  4. Container With Most Water

Advanced

  1. Three Sum
  2. Four Sum
  3. Trapping Rain Water
  4. Sliding Window Problems

Final Thoughts

The Two Pointer Pattern is one of the most useful techniques in DSA. It helps reduce time complexity, avoid nested loops, and solve many array and string problems efficiently.

Whenever you encounter a problem involving:

  • Arrays
  • Strings
  • Pairs of elements
  • Sorted data
  • Constant space constraints

consider whether a Two Pointer solution is possible.

Mastering this pattern will make many coding interview problems significantly easier to solve.

Share: X / Twitter LinkedIn Reddit WhatsApp

Comments

Questions, corrections, and practical takeaways are welcome here.