Two Pointers Algorithm – Intuition & Patterns

Two Pointers Algorithm – Intuition & Patterns

🧠 What is the Two Pointers Technique?

The two pointers algorithm uses two indices that move towards each other (or in the same direction) to scan through data — typically an array or string — to find solutions faster than nested loops.

I like to call this the squeeze algorithm — because the pointers “squeeze” the problem space from both ends until the condition is met.


🔍 Where is it Used?

  • Sorted arrays (common in questions asking for pair sums)
  • Strings or arrays with substructure (e.g., palindromes, reverse operations)
  • Finding or optimizing subarrays or subsequences
  • Eliminating nested loops when scanning with start-end pairs

🧩 Types of Movement

  1. Same Direction

    • Start both pointers at the beginning
    • Example: slow/fast runners, merging sorted arrays
  2. Opposite Direction

    • One pointer at start, the other at end
    • Ideal for sum-based or palindrome problems

🪄 Why it Works

  • Reduces time complexity from O(n²) to O(n) in many problems
  • Works well with sorted data or problems where left/right symmetry helps
  • Simple to implement but powerful when identifying the correct window or combination

💡 Key Insight

If a brute-force solution requires nested loops comparing two values at a time — think if those comparisons can be done by moving two pointers instead.

✅ Efficient
✅ Clean logic
✅ Lower space usage