awelgorithms

Shrink the window by moving left

Updated

Shrink is: remove a[L], then L += 1. R stays. The new window is a[L..R] with the new L.

What to focus on

Only L moves forward. Read the old a[L] before incrementing L. Require a non-empty window (L ≤ R) before you shrink.

Worked examples

drop first

a = [4, 1, 9], L=0 R=2
  1. remove a[0]=4
  2. L += 1 → 1
  3. R stays 2

Result: L=1 R=2 · [1, 9]

drop again

a = [4, 1, 9], L=1 R=2
  1. remove a[1]=1
  2. L += 1 → 2

Result: L=2 R=2 · [9]

from four

a = [4, 1, 9, 1], L=0 R=3
  1. remove a[0]=4
  2. L += 1 → 1

Result: L=1 R=3 · [1, 9, 1]

Try it

Write shrink(a, L, R). a is an array of integers. L is the left index. R is the right index. Shrink removes a[L] then L += 1. The window then includes the new a[L] through a[R]. Print (L, R) after each shrink. Return nothing. On [4, 1, 9] from (0, 2) then (1, 2); [4, 1, 9, 1] from (0, 3).

a is nonempty. Start with L ≤ R so the window has a leftmost cell.