Never let the trail pass the lead
Updated
Same-direction work requires L ≤ R after every move. If a catch-up would make L = R+1, stop. The trail does not read past the lead.
What to focus on
Before L += 1, require L < R (or L ≤ R if they may meet and then stop). After any accepted move, L ≤ R still holds.
Worked examples
legal meet
a = [4, 1, 9], L=1, R=2- start L=1, R=2
- L += 1
Result: L=2, R=2 · still L ≤ R
refuse pass
a = [4, 1, 9], L=2, R=2- start L=2, R=2
- L += 1 would be 3 > R
Result: L=2, R=2 · refuse
empty
a = []- no pointers
- there is nothing to pass
Result: no L, R
Try it
Write guardTrail(a, L, R). a is an array of integers. L is the trail index. R is the lead index. Accept L += 1 only if L < R. After every accepted move, L ≤ R. Print L and R. On [4, 1, 9] from (1, 2) accept, from (2, 2) refuse, and on [4, 1, 9, 1] from (1, 3) accept.
a may be empty. L and R are in-range with L ≤ R at the start of the check.