PAVANXDCL • DSA FOUNDATION

Master Two Pointers & Binary Search

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 Pointer Template

Intro + Two Pointers Template

▶
LOGIC

Two Pointers + Binary Search

3Sum + Intersection of 2 Arrays

▶
ARRAYS

Two Pointer + 2Sum II

Build the classic sorted-array solution.

▶
STRINGS

Sliding Window of size K [Fixed Window

Use pointers from both ends.

▶
ADVANCED

Search Insert Position [Binary Search]

Understanding 2 pointers in 2 arrays

▶
ADVANCED

Merge Sorted Array[Binary Search]

Understanding 2 pointers in 2 arrays

▶
ADVANCED

Maximum Average Subarray I

Understanding 2 pointers in 2 arrays

▶
ADVANCED

Find the starting and ending index of the number in sorted array

Understanding 2 pointers in 2 arrays