Algorithms

Two Pointers (Intro)

Difficulty: Beginner

Given a sorted array and a target sum, the brute-force "check every pair" approach is O(n^2). With two indices walking inward from opposite ends, the same problem drops to O(n): if the current pair sums too low, move the left pointer...

Learn
/
Algorithms
/

Two Pointers (Intro)

0%
BEGINNER
Complexity varies

Two Pointers (Intro)

Given a sorted array and a target sum, the brute-force "check every pair" approach is O(n^2). With two indices walking inward from opposite ends, the same problem drops to O(n): if the current pair sums too low, move the left pointer right; if it sums too high, move the right pointer left.

Two Pointers (Intro) turns that insight into a reusable pattern with two main shapes. The opposite-direction variant has pointers converge from both ends and powers problems like pair sum, palindrome check, reverse in place, and container with most water. The same-direction (fast and slow) variant has both pointers walk forward at different speeds and powers in-place filtering problems like remove duplicates from a sorted array, move zeros to the end, and the linked-list cycle detection you will revisit later.

In Arrays & Strings, you learned how a single index walks across an array. This lesson coordinates two indices in a way that exploits sorted order or a structural invariant to skip work an inner loop would otherwise repeat.

Next, Sliding Window (Intro) generalizes the same-direction pointer pair into an expanding and shrinking range that solves contiguous-subarray problems.

Beginner
55 min
6 Objectives
5 Sections

Prerequisites:

Arrays & Strings

Topics:

Algorithms
Two Pointers
Arrays
Fast/Slow Pointers
Patterns
Time Complexity
Beginner
Free

What You'll Learn

By the end of this lesson, you will be able to:

Explain how two pointers reduce O(n^2) brute-force searches to O(n) by leveraging sorted order.

Implement the opposite-direction (converging) two-pointer pattern to solve pair-sum and palindrome problems.

Implement the same-direction (fast/slow) two-pointer pattern to remove duplicates and partition arrays in place.

Trace two-pointer movement step by step, predicting pointer positions after each iteration.

Identify when two pointers is the right technique for a given problem.

State the time and space complexity of two-pointer solutions.

Why This Matters

01

Two pointers reduce O(n^2) brute-force pair/subarray searches to O(n) by exploiting sorted order or structural invariants.

02

It is one of the most frequently tested interview patterns — appearing in roughly 15-20% of array and string problems.

03

The fast/slow pointer variant is the foundation for linked-list cycle detection, which you will encounter in later lessons.

04

Mastering two pointers builds intuition for the sliding window technique, which extends the same idea to variable-width ranges.

Key Terms

7 terms you'll encounter in this lesson

1

Two pointers

A technique that uses two index variables traversing an array in a coordinated manner to solve problems in O(n) time instead of O(n^2).

2

Opposite-direction pointers

A two-pointer variant where one pointer starts at the beginning and another at the end, and they move inward toward each other.

3

Same-direction pointers (fast/slow)

A two-pointer variant where both pointers start at the same end and move in the same direction at different speeds.

4

Converge

When opposite-direction pointers move toward each other until they meet or cross, indicating all pairs have been examined.

5

In-place

An algorithm that transforms data using O(1) extra space, modifying the input array directly rather than creating a new one.

6

Invariant

A condition that remains true throughout the algorithm's execution. For two pointers on a sorted array, the invariant is that the answer (if it exists) lies between the two pointers.

7

Partition

Rearranging array elements so that elements satisfying a condition come before those that do not — a common use of same-direction pointers.

Heads Up

Common misconceptions to watch out for

"Two pointers only works on sorted arrays"

Opposite-direction two pointers for pair-sum requires sorted data, but same-direction (fast/slow) pointers work on unsorted arrays too. For example, 'move zeroes to end' does not require sorting.

"Two pointers always uses one pointer at each end"

That is only the opposite-direction pattern. The same-direction pattern uses both pointers starting from the left, moving at different speeds — like the fast/slow pattern for removing duplicates.

"Two pointers is the same as binary search because both use two indices"

Binary search uses left/right to narrow a search space by halving. Two pointers move both indices based on comparison results without halving. Binary search is O(log n); two pointers is O(n).