PAVANXDCL • DSA FOUNDATION

Master Two Pointers

Learn how two indices can reduce repeated work and turn many array and string problems into clean O(n) solutions.

01 — FOUNDATION

What is Two Pointers?

Two pointers means maintaining two positions and moving them according to a proven rule.

←→

Two Indices

One pointer can start left and another right, or both can move forward.

⚡

Reduce Work

Instead of checking every pair, pointer movement can eliminate large parts of the search space.

🎯

Use the Constraint

Sorted order, boundaries or a monotonic condition usually tells you which pointer can move safely.

02 — CORE LOGIC

How the Technique Works

The important part is not having two variables. It is knowing why a pointer can move without losing the answer.

LeftStart or slow pointer
→
ConditionCompare values or track a constraint
→
MoveAdvance left, right, or both
03 — VISUALIZATION

Left + Right Pointer

Example: find two values whose sum equals a target in a sorted array.

0
1
LEFT
1
2
2
4
3
6
4
8
5
11
RIGHT

If left + right is too small → move LEFT. If too large → move RIGHT.

04 — COMMON PATTERNS

Three Two-Pointer Styles

Opposite Ends

Left starts at 0 and right at n−1. Common for sorted two-sum and palindrome problems.

Same Direction

Both pointers move forward. Common for filtering, partitioning and removing duplicates.

Fast + Slow

One pointer moves faster. Common in linked lists for cycles and middle-node problems.

Decision Rule

Pointer movement must reduce the search space while preserving correctness.

05 — RECOGNITION

When Should You Think Two Pointers?

SignalTechniqueExample
Sorted array + target pairOpposite pointersTwo Sum II
Check from both endsOpposite pointersValid Palindrome
Remove duplicates in-placeSame-directionRemove Duplicates
Move elements by conditionTwo pointersMove Zeroes
Linked-list cycleFast + slowFloyd Cycle Detection
06 — VIDEO LEARNING

Watch Videos

▶
BEGINNER

Two Pointers Explained

Intro + Two Pointers Template

▶
LOGIC

WarmUp Problems

3Sum + Intersection of 2 Arrays

▶
ARRAYS

4Sum

Build the classic sorted-array solution.

▶
STRINGS

Reverse Vowels of a String

Use pointers from both ends.

▶
ADVANCED

Is Subsequence

Understanding 2 pointers in 2 arrays