Data Structures19 sections · 729 units
Open in Course

Lessons from Two Pointers

When to use it

What did you learn?

1.1. Two pointers works on sorted arrays where you're searching for pairs satisfying some condition.

2.2. The trick is that moving one pointer has a predictable effect. You can eliminate candidates without checking them.

3.3. No extra space needed. You're just moving indices. Two pointers appears in many forms: finding pairs, removing duplicates, partitioning arrays. The pattern stays the same.