Update window state on exclude
Updated
On exclude, fold a[L] out of state: sum -= a[L], then L += 1. Do not rescan the slice.
What to focus on
Window state here is running sum. After exclude, sum equals a[L]+…+a[R] on the new bounds. Subtract the old a[L], not the new one.
Worked examples
drop 4 from sum
a = [4, 1, 9], L=0 R=2, state=14- state -= a[0]=4
- L += 1
Result: state = 10 · [1, 9]
drop 1 from sum
a = [4, 1, 9], L=1 R=2, state=10- state -= a[1]=1
- L += 1
Result: state = 9 · [9]
four-cell drop
a = [4, 1, 9, 1], L=0 R=3, state=15- state -= a[0]=4
- L += 1
Result: state = 11 · [1, 9, 1]
Try it
Write excludeSum(a, state). a is an array of integers. state is the running sum of a[L..R]. L is the left index to drop. Subtract a[L], then L += 1. The window then excludes that cell. Return the new state. On [4, 1, 9] from state 14 then 10; [4, 1, 9, 1] from state 15.
a is nonempty. All values are integers. L ≤ R. state is the aggregate of a[L..R].