awelgorithms

Window length is right minus left plus one

Updated

Length = R - L + 1 when L ≤ R. Count the cells, inclusive both ends. Do not count the gap between the edges.

What to focus on

Inclusive length is R-L+1. Exclusive-right [L,R) would be R-L. Mixing the two is the off-by-one.

Worked examples

three cells

a = [4, 1, 9, 1], L=1 R=3
  1. R-L+1 = 3-1+1 = 3
  2. indices 1,2,3

Result: length 3

one cell

a = [7], L=0 R=0
  1. 0-0+1 = 1
  2. the single index 0

Result: length 1

string window

s = "abca", L=0 R=2
  1. 2-0+1 = 3
  2. chars a,b,c

Result: length 3

Try it

Write length(L, R). L is the left index. R is the right index with L ≤ R. The window includes both ends, so length is R-L+1. Return R-L+1. Contrast exclusive [0, 3) on n=4, which has length 3 not 4. On a=[4, 1, 9, 1] (1, 3); a=[7] (0, 0); s="abca" (0, 2).

a (or s) is nonempty. L ≤ R.