awelgorithms

Fixed length pairs include with exclude

Updated

Every slide is a pair: exclude the outgoing left cell and include the incoming right cell. Do both in the same step. Length stays k.

What to focus on

Include without exclude grows past k. Exclude without include shrinks below k. Fixed-k is those two updates together, not one of them.

Worked examples

paired step

a = [1, 2, 3, 4], k=2, at i=2
  1. exclude a[0]=1
  2. include a[2]=3
  3. length stays 2

Result: [2, 3]

include only bug

a = [1, 2, 3, 4], k=2, add a[2] and skip the drop
  1. window becomes [1,2,3]
  2. length 3 ≠ k

Result: broken length 3

exclude only bug

a = [4, 1, 9], k=2, drop 4 and skip the add
  1. window becomes [1]
  2. length 1 ≠ k

Result: broken length 1

Try it

Write slidePair(a, k). a is an array of integers. k is the fixed length. Each step excludes the old left cell and includes the new right cell together. Length stays k. Print the window after the paired step. Then show the length bugs if you include-only or exclude-only. On [1, 2, 3, 4] k=2 at i=2; [4, 1, 9] k=2 at i=2.

a is nonempty. 1 ≤ k < n so a slide exists. The paired step starts from a seeded window of length k.