awelgorithms

Advance the lead pointer first

Updated

The lead is R. Same-direction scans move it first: R += 1. The trail L stays. You look ahead or grow a gap before the trail catches up.

What to focus on

After the move, R is the old R+1, L is unchanged, and L ≤ R. R stays < n.

Worked examples

from together

a = [4, 1, 9], L=0, R=0
  1. start L=0, R=0
  2. R += 1

Result: L=0, R=1

again

a = [4, 1, 9], L=0, R=1
  1. start L=0, R=1
  2. R += 1

Result: L=0, R=2

blocked

a = [4, 1, 9], L=0, R=2
  1. start L=0, R=2
  2. R += 1 would be 3, out

Result: L=0, R=2 · refuse

Try it

Write advanceLead(a, L, R). a is an array of integers. L is the trail index. R is the lead index. Do R += 1. Print L and R after the move. Return nothing. On [4, 1, 9] from (0, 0) then (0, 1), and on [4, 1, 9, 1] from (0, 2).

a has length ≥ 3. R starts strictly before the last index.