awelgorithms

Exclude the old leftmost cell

Updated

Exclude means the cell at the old L leaves the window. After L += 1 that cell is strictly left of a[L..R].

What to focus on

Exclude a[old L], not a[L+1]. Order: name a[L], then increment. Increment-first then exclude a[L] drops the wrong cell.

Worked examples

exclude 4

a = [4, 1, 9], L=0 R=2
  1. old leftmost is index 0
  2. cell 4 leaves
  3. 1 and 9 stay

Result: excluded 4

exclude 1

a = [4, 1, 9, 1], L=1 R=3
  1. old leftmost is index 1
  2. cell 1 leaves

Result: excluded 1

order bug

a = [4, 1, 9], L=0 R=2, L++ then drop a[L]
  1. L becomes 1 first
  2. a[1]=1 is dropped instead of 4

Result: wrong exclude · dropped 1

Try it

Write excludeLeft(a, L). a is an array of integers. L is the current left index. R is unchanged. The window currently includes a[L]. Name a[L] first, then L += 1. Print the dropped cell. Then show the L++-first bug on [4, 1, 9]. Return nothing. On [4, 1, 9] at L=0; [4, 1, 9, 1] at L=1.

a is nonempty. L ≤ R so a[L] is inside the window.