Initialize right at the last index
Updated
Right is the last legal index. R ← n-1. Compute it from length. Do not scan to the end to find it.
What to focus on
R = n-1. R is never n. For n = 1, first and last are both 0.
Worked examples
three
a = [4, 1, 9]- n = 3
- R ← 2
- a[2] = 9
Result: R=2
one
a = [7]- n = 1
- first and last are both 0
Result: R=0
empty
a = []- n = 0
- n-1 = -1 is not a valid index
Result: no R
Try it
Write initRight(a). a is an array of integers. R is the right index. Set R = n-1. Print R. Return nothing. On [4, 1, 9] R=2. On [4, 1, 9, 1] R=3. On [7] R=0.
a may be empty. Length ≥ 1.