awelgorithms

Include the new rightmost cell

Updated

After R += 1, the cell a[R] now sits in the window. It was outside. Now it is inside. Name that cell.

What to focus on

Include means the new R is part of a[L..R]. You do not include a[R+1]. You do not skip a[R]. L is unchanged.

Worked examples

include first

a = [4, 1, 9], L=0, R from -1 to 0
  1. new rightmost index is 0
  2. cell 4 enters

Result: window holds 4

include next

a = [4, 1, 9], L=0, R from 0 to 1
  1. new rightmost index is 1
  2. cell 1 enters
  3. 4 stays

Result: window holds 4, 1

include on a string

s = "abca", L=0, R from 1 to 2
  1. new rightmost index is 2
  2. char c enters

Result: window holds a, b, c

Try it

Write includeRight(a, L, R). a is an array of integers, or s is a string. L is the left index. R is the new right index after expand. The window now includes a[R]. Print the cell that entered. Return nothing. L is unchanged. On [4, 1, 9] expanding R=-1→0 then 0→1; "abca" expanding R=1→2.

a (or s) is nonempty. The expand R += 1 stays in range.