awelgorithms

Leave a one-cell gap between pointers

Updated

A one-cell gap means R = L+2: one index sits strictly between the ends. That middle cell is not an end. Some reverses and partitions leave it unread.

What to focus on

After the move, R - L == 2 and the middle index L+1 is neither L nor R. You do not process a[L+1] as an end.

Worked examples

three

a = [4, 1, 9], L=0, R=2
  1. L=0, R=2
  2. middle index 1 holds 1
  3. ends 4 and 9

Result: L=0, R=2 · gap cell 1

four

a = [4, 1, 9, 1], L=0, R=3
  1. start L=0, R=3
  2. advance only R
  3. R=2

Result: L=0, R=2 · gap cell 1

one

a = [7]
  1. only one index
  2. R = L+2 is out of range

Result: no gap

Try it

Write oneCellGap(a, L, R). a is an array of integers. L is the left index. R is the right index with R = L+2. Print the unread middle index L+1. Return nothing. On [4, 1, 9] at (0, 2) the gap cell is 1. On [4, 1, 9, 1] after R-- from (0, 3) the gap cell is 1. On [4, 1, 9, 1, 7] at (1, 3) the gap cell is 2.

a has length ≥ 3. L and R are in range with R = L+2.