awelgorithms

Seed the first k cells

Updated

The first window is [0, k): inclusive L=0, R=k-1. Include a[0]..a[k-1] into state before any slide.

What to focus on

You must seed k cells, not k-1. After seed, state is exactly a[0..k-1].

Worked examples

seed two

a = [1, 2, 3, 4], k=2
  1. include a[0]=1
  2. include a[1]=2
  3. R=1

Result: first window [1, 2] · sum 3

seed two of three

a = [4, 1, 9], k=2
  1. include 4 then 1
  2. R=1

Result: first window [4, 1] · sum 5

cannot seed

a = [7], k=2
  1. need indices 0 and 1
  2. n=1 has no index 1

Result: no first window

Try it

Write seed(a, k). a is an array of integers. k is the fixed length. L = 0. R = k-1. The first window includes a[0..k-1] when k ≤ n. Return the seeded sum, or report that there is no first window when k > n. On [1, 2, 3, 4] k=2 → sum 3. On [4, 1, 9] k=2 → sum 5. On [7] k=2 → no first window.

a is nonempty. k ≥ 1. When k > n there is no first window.