Maintain write index21.07
The write index w is the next free slot in the compacted prefix. It starts at 0 and only increases when you keep a value.
Invariant: a[0..w) is already the final prefix. a[w..] is unread or leftover. i is the read index, i>=w always in the usual compact.
Same skill, three traces. Read all three before you try it.
01start - given
a = [0, 4, 0, 1],w = 0,i = 0- walk
prefix empty
- result
- nothing committed
02keep - given
a = [0, 4, 0, 1],a[i]=4,a[w]=4,w++- walk
prefix
[4]- result
w=1
03skip - given
a = [0, 4, 0, 1],a[i]=0,wstays- walk
prefix unchanged
- result
w=1still
compactNonZero(a).a is an array of integers. i is the read index. w is the write index.For zeros-compact on
[0, 4, 0, 1], print (i, w) after each i, and print the committed prefix a[0..w).constraintsa is non-empty.
- Move Zeroes0/3 skills ready · arrays · easyopen
- Move Zeroes0/3 skills ready · arrays · easyopen
Every Check with Awel is frozen here. Open one to read the code or load it back into the editor.