Keep a fixed gap between pointers
Updated
A fixed gap means R - L stays constant. When you advance, move both: L += 1 and R += 1. Adjacent walkers use gap 1.
What to focus on
After a joint step, R - L equals the original gap. Both stay in range. Stop when R+1 would equal n. Do not move L alone and shrink the gap.
Worked examples
gap 1
a = [4, 1, 9], L=0, R=1- start L=0, R=1
- L += 1, R += 1
Result: L=1, R=2 · gap 1
gap 2
a = [4, 1, 9, 1], L=0, R=2- start L=0, R=2
- L += 1, R += 1
Result: L=1, R=3 · gap 2
blocked
a = [4, 1, 9], L=1, R=2- start L=1, R=2
- R += 1 would be 3, out
Result: L=1, R=2 · refuse
Try it
Write fixedGap(a, L, R). a is an array of integers. L is the left index. R is the right index. Gap is R - L and must stay constant. Do L += 1 and R += 1 together. Print L and R. On [4, 1, 9] from (0, 1), on [4, 1, 9, 1] from (0, 2), and from (1, 2).
a has length ≥ 3. R+1 < n so the joint step stays in range.