Back to Intelligence Feed
Algorithms 9 min read April 22, 2026

The Two Pointers Technique: More Powerful Than You Think

N

Nawajish Alam

Arena Strategist

If you ask most people to describe the two-pointer technique, they'll say "use one pointer from the start and one from the end of a sorted array." That's true, but it undersells the pattern significantly. Two pointers is really about maintaining two independent positions in your data — and the relationship between them drives your logic.

The Three Flavors

Converging pointers: Start at opposite ends, move toward each other. Classic use: Two Sum on a sorted array, checking if a string is a palindrome. The invariant is that you're narrowing down a range.

Same-direction pointers (fast/slow): Both start at the beginning, but move at different speeds. This is Floyd's cycle detection algorithm — the tortoise and hare. It's also how you find the middle of a linked list in one pass, or detect if a linked list has a cycle without extra memory.

Sliding window (a special case): One pointer defines the start of a window, the other defines the end. This is technically two pointers — the sliding window technique is built on top of the two-pointer concept.

Why It Works: The Key Insight

Two pointers eliminates the need to re-examine pairs or elements you've already ruled out. In a sorted array Two Sum, if you have a pointer at index i and j and their sum is too large, you know immediately that any pair involving arr[j] with anything to the right of i is also too large — so you move j left. You've eliminated an entire column of pairs in one step.

That's where the O(n²) → O(n) improvement comes from. It's not magic; it's structured elimination.

A Problem That Surprised Me

I once saw a problem: "Given an array of integers, remove duplicates in-place and return the new length." My first instinct was to reach for a Set, but the problem said O(1) extra space.

Two pointers solves it cleanly. Keep a "write" pointer that tracks where the next unique element should go. Use a "read" pointer to scan forward. When you find an element different from the one at write-1, copy it to the write position and advance both. This is so common in practice that it appears in array compaction routines in many standard libraries.

On Linked Lists Specifically

Two pointers shines on linked lists because you can't index directly. Finding the kth node from the end? Move one pointer k steps ahead, then advance both until the lead hits the end — the trailing pointer is at your target. Detecting a cycle? Fast/slow pointers. Finding the start of a cycle? Involves a mathematical insight about where they meet that's genuinely elegant to work through once.

Practice Recommendation

Try solving: Valid Palindrome, Container With Most Water, 3Sum, and Linked List Cycle II. Each one exercises a different flavor of the pattern. After those four, you'll have solid intuition for when to reach for two pointers and which flavor fits the problem.

ELIX // ARTICLE_RECAP // END_TRANSMISSION