Two Pointers vs Sliding Window

How to distinguish these related techniques.

Two pointers:

  • Pointers move independently
  • Can move opposite directions
  • Focus on pairs or partitioning
  • Examples: Two Sum, 3Sum, Container

Sliding window:

  • Contiguous subarray/substring
  • Left and right form bounds
  • Usually both move right
  • Examples: Max sum, longest substring

Key difference: Two pointers for pairs, sliding window for contiguous ranges. Sliding window is a specialization of two pointers.