awelgorithms

Name the right edge of a window

Updated

R is the rightmost index in the window. a[R] is the cell sitting there. Inclusive-inclusive: R is inside, R+1 is not.

What to focus on

Every index > R is outside. For a non-empty window 0 ≤ L ≤ R ≤ n-1.

Worked examples

right of a middle window

a = [4, 1, 9, 1], L=0 R=2
  1. right edge is index 2
  2. a[2] = 9
  3. index 3 is outside

Result: R = 2

right of one cell

a = [7], L=0 R=0
  1. n = 1
  2. the only legal right edge is 0

Result: R = 0

right of a string window

s = "abca", L=0 R=2
  1. right edge is index 2
  2. s[2] = c

Result: R = 2

Try it

Write rightEdge(L, R). L is the left index. R is the right index. The window includes a[L] through a[R]. R is inclusive. Return R. On a=[4, 1, 9, 1] with (0, 2); a=[7] with (0, 0); s="abca" with (0, 2).

a (or s) is nonempty. 0 ≤ L ≤ R < n.