awelgorithms

Left never passes right in a window

Updated

After shrink, L ≤ R+1. Empty is L = R+1 (equivalently R < L). L may equal R (one cell). L must not jump to R+2 and leave a hole of un-excluded cells.

What to focus on

Stop shrinking when the window is already empty, or when you still need the one remaining cell. Passing empty (L > R+1) desyncs state from [L,R].

Worked examples

meet at one cell

a = [4, 1, 9], L=2 R=2
  1. L == R
  2. window is [9]
  3. next shrink would make empty, not L=4

Result: L=2 R=2 legal

empty is L = R+1

a = [4, 1, 9], L=3 R=2
  1. L = R+1
  2. no cell remains
  3. do not L++ again to 4

Result: empty · L=3 R=2

one-cell empty

a = [7], L=0 R=0 then shrink once
  1. exclude 7
  2. L += 1 → 1
  3. R stays 0 so L = R+1

Result: empty · L=1 R=0

Try it

Write assertBounds(L, R). L is the left index. R is the right index. Empty is L = R+1. One-cell is L == R. Reject L > R+1. Print legal or reject. Return nothing. On [4, 1, 9] at (2, 2) and (3, 2); [7] shrink from (0, 0) to (1, 0).

a is nonempty. L and R come from expand/shrink so L ≤ R+1.