Two pointers, sliding windows and prefix sums

Many problems ask about a stretch of a list: a pair of positions, a run of consecutive items, the sum between two indices. The obvious answer is a loop inside a loop, one for each end of the stretch, which is quadratic. These three techniques replace that with a single pass by moving the ends cleverly.

Two pointers on sorted data

In a sorted list, find two numbers that add up to a target. Put one finger on the smallest and one on the largest. If their sum is too small, the only way to make it bigger is to move the left finger right. Too big, move the right finger left. Each step rules out one position for good.

def pair_with_sum(items, target):
    left, right = 0, len(items) - 1
    while left < right:
        total = items[left] + items[right]
        if total == target:
            return [left, right]
        if total < target:
            left += 1
        else:
            right -= 1
    return []

print(pair_with_sum([1, 2, 4, 7, 11, 15], 15))
print(pair_with_sum([1, 2, 4, 7, 11, 15], 30))

Each finger moves at most n times, so the whole thing is linear. You wrote this as two sum, sorted; it is also behind most water and the three-sum family.

The other two-pointer shape is slow and fast: both start at the left, the fast one scans, the slow one marks where the next kept item goes. That is how you remove duplicates from a sorted list in place or move all zeros to the end without a second list:

def dedupe_sorted(items):
    if not items:
        return 0
    slow = 0
    for fast in range(1, len(items)):
        if items[fast] != items[slow]:
            slow += 1
            items[slow] = items[fast]
    return slow + 1

items = [1, 1, 2, 3, 3, 3, 5]
n = dedupe_sorted(items)
print(n, items[:n])

slow always points at the last unique value written; everything before it is the answer so far.

A window that slides

"The largest total over any three consecutive days" invites summing every window of three, which costs 3n, or kn for a window of k. A sliding window keeps a running total: when the window moves one step right, add the item that entered and subtract the item that left.

def best_window(daily, k):
    total = sum(daily[:k])
    best = total
    for i in range(k, len(daily)):
        total += daily[i] - daily[i - k]
        best = max(best, total)
    return best

print(best_window([2, 1, 5, 1, 3, 2], 3))

One pass, however large k is. This was best stretch of days.

The window does not have to be a fixed size. For "the longest run with no repeated character", the right end advances one character at a time and the left end jumps forward only when a repeat appears:

def longest_unique(text):
    last_seen = {}
    start = 0
    best = 0
    for i, ch in enumerate(text):
        if ch in last_seen and last_seen[ch] >= start:
            start = last_seen[ch] + 1
        last_seen[ch] = i
        best = max(best, i - start + 1)
    return best

print(longest_unique("abcabcbb"))
print(longest_unique("bbbbb"))
print(longest_unique("pwwkew"))

The window is text[start:i + 1], and the rule that keeps it valid is "when ch was seen inside the window, move start past that sighting". Both ends only ever move right, so the total work is linear. Variable windows all follow this outline: grow from the right, shrink from the left until the window is valid again, record the best.

Prefix sums

Asked many times "what is the sum from index i to index j?", summing each time is linear per question. Instead compute once a list of running totals, where prefix[k] is the sum of the first k items. Then any range sum is one subtraction:

def prefix_sums(items):
    prefix = [0]
    for x in items:
        prefix.append(prefix[-1] + x)
    return prefix

sales = [3, 1, 4, 1, 5, 9, 2]
prefix = prefix_sums(sales)
print(prefix)

def range_sum(i, j):
    return prefix[j + 1] - prefix[i]

print(range_sum(2, 4), sum(sales[2:5]))
print(range_sum(0, 6), sum(sales))

prefix has one more entry than sales, starting with 0, so that the sum of sales[i..j] is prefix[j + 1] - prefix[i] with no special case for i == 0. Building it is linear once; every question after that is constant. You met it as prefix sums.

Prefix sums also turn "how many stretches sum to exactly k" into a hashing problem: a stretch ending at j sums to k when prefix[j + 1] - k has appeared as an earlier prefix, so count earlier prefixes in a dictionary while walking.

Choosing between them

  • Sorted input and a question about pairs: two pointers from the ends.
  • Rearranging in place, keeping some items: slow and fast pointers.
  • A question about consecutive items with a fixed length: fixed window.
  • "Longest" or "shortest" consecutive run satisfying a condition: a variable window, if making the window bigger only makes the condition harder (or only easier). If the condition can flip back and forth as the window grows, a window does not work.
  • Many sum queries over ranges: prefix sums.

Try it

Range sums

Write range_sums(items, queries) where queries is a list of (i, j) pairs with i <= j. Return a list with, for each query, the sum of items[i] through items[j] inclusive.

range_sums([3, 1, 4, 1, 5], [(0, 2), (1, 3), (4, 4)])  ->  [8, 6, 5]
range_sums([3, 1, 4, 1, 5], [])                        ->  []

The point is to practise prefix sums. One check has a hundred thousand items and a hundred thousand queries; summing each range directly is billions of steps and will not finish in time.

def range_sums(items, queries):
    ...

Shortest window with enough

nums is a list of positive whole numbers. Write shortest_window(nums, target) that returns the length of the shortest run of consecutive items whose sum is at least target, or 0 if no run reaches it.

shortest_window([2, 3, 1, 2, 4, 3], 7)  ->  2     (4 + 3)
shortest_window([1, 1, 1], 5)           ->  0
shortest_window([5], 5)                 ->  1

The point is to practise a variable-size sliding window: grow the right end until the sum is enough, then shrink from the left while it still is.

def shortest_window(nums, target):
    ...

Closest pair sum

items is a sorted list of at least two whole numbers. Write closest_pair_sum(items, target) that returns the sum of the two different items whose sum is closest to target. If two sums are equally close, return the smaller sum.

closest_pair_sum([1, 3, 5, 8, 12], 10)   ->  9      (1 + 8; 11 is equally close but larger)
closest_pair_sum([1, 3, 5, 8, 12], 20)   ->  20     (8 + 12)
closest_pair_sum([-4, -1, 2, 6], 0)      ->  1      (-1 + 2)

The point is to practise two pointers from both ends of a sorted list.

def closest_pair_sum(items, target):
    ...

Next: Linked lists.