awelgorithms

Advance only the right end

Updated

Advancing only right is R -= 1. L stays. You do this when a[R] is finished and a[L] is still useful.

What to focus on

After the move, R is the old R-1 and L is unchanged. The new R is still ≥ L.

Worked examples

first step

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

Result: L=0, R=1

meet

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

Result: L=0, R=0

one

a = [7], L=0, R=0
  1. start L=0, R=0
  2. R -= 1 would be -1

Result: L=0, R=0 · refuse

Try it

Write advanceRight(a, L, R). a is an array of integers. L is the left index. R is the right index. Do R -= 1. Print L and R after the move. Return nothing. On [4, 1, 9] from (0, 2) then (0, 1), and on [4, 1, 9, 1] from (0, 3).

a has length ≥ 2. Start with L < R so the new R is still in range.