awelgorithms

Decide two pointers versus a window

Updated

A window is a contiguous a[L..R] whose contents (sum, distinct count) must stay valid. L only grows to repair it. Opposite-end pair-sum is not a window: the pair skips the middle. Compact is not a window: dropped slots leave the logical prefix, not a sliding slice.

What to focus on

Contiguous constraint + monotonic L → window. Pair from ends, or in-place filter, or two sorted lists → two pointers.

Worked examples

window

a = [4, 1, 9], longest sum ≤ 10
  1. [4,1] sum 5
  2. grow to [4,1,9] sum 14 invalid, shrink L

Result: window

two pointers

a = [1, 4, 9], t=13 pair
  1. 1+9 skips 4
  2. not contiguous

Result: opposite-end two pointers

compact not window

a = [0, 1, 0, 9], keep nonzero
  1. i and w both start at 0
  2. logical [1, 9] was not a slice of the input

Result: read/write two pointers

Try it

Write classify(a). a is an array of integers. A window is contiguous a[L..R]. Opposite-end pair-sum skips the middle. Print window or two pointers. Return nothing. For a = [4, 1, 9] longest sum ≤ 10, a = [1, 4, 9] pair sum 13, and a = [0, 1, 0, 9] compact zeros.

a is nonempty. Window sums use positive values. Pair-sum a is sorted and a target pair exists.