awelgorithms

Initialize left at the start

Updated

Left is the start index. L ← 0. You compute it. You do not walk the array to discover the first slot.

What to focus on

L is 0 whenever n ≥ 1. L is not 1. Do not set L to n.

Worked examples

three

a = [4, 1, 9]
  1. n = 3
  2. L ← 0
  3. a[0] = 4

Result: L=0

one

a = [7]
  1. n = 1
  2. the only legal index is 0

Result: L=0

empty

a = []
  1. n = 0
  2. index 0 does not exist

Result: no L

Try it

Write initLeft(a). a is an array of integers. L is the left index. Set L = 0. Print L. Return nothing. L must be 0 on every run. On [4, 1, 9], [4, 1, 9, 1], and [7], L is 0.

a may be empty. Length ≥ 1.