The two-pointer method is an invariant wearing running shoes. You keep two indices — often left and right — and you only move them forward. Each element enters and leaves the window a constant number of times, so the whole scan is linear.
It is not a magic pair of indices. It works when there is a reason you never need to move a pointer backwards.
function twoSumSorted(a, target) {
let L = 0;
let R = a.length - 1;
while (L < R) {
const s = a[L] + a[R];
if (s === target) return [L, R];
if (s < target) L++;
else R--;
}
return null;
}Sliding windows are two pointers plus a payload: the sum of the window, a frequency map, the number of unique characters, a max deque. You expand right every step, and you shrink left while the window is illegal. Same O(n) tax.
Checks 0/2