Two Pointers — Converging

Sorted array, target sum. Put one pointer at each end and walk them toward each other, deciding which to move by comparing the current sum to the target.

When to reach for it: the input is sorted (or you can sort it), and you’re looking for a pair — or a fixed-size combination — that hits, brackets, or counts against a target sum.

The micropattern:

  1. Sort the array (if it isn’t already).
  2. Place left at the start and right at the end.
  3. Look at nums[left] + nums[right]:
    • too small? move left right to grow the sum.
    • too big? move right left to shrink it.
    • just right? record it and step both inward.

For 3-/4-sum variants you fix the outer element(s) with a loop and run the converging scan on the rest. Work through the problems below in order — each one adds a small twist.