Facebook IconTwitter IconReddit IconLinkedIn IconPinterest Icon

Two Pointer Technique

The Two Pointer Technique is a powerful algorithmic strategy used to solve problems involving arrays, strings, or linked lists efficiently. By using two pointers that traverse data structures in a coordinated manner, we can reduce time complexity from O(n²) to O(n) in many cases.

What is the Two Pointer Technique?

The technique involves using two pointers (indices or references) that traverse a data structure in a specific manner to solve a problem optimally.

The two pointers can move:

  • Towards each other (one from start, one from end)
  • In the same direction (both from start, but at different speeds)
  • In alternating patterns (e.g., one moves while the other stays)