Algorithm Search⌘K

Proofs: Sliding window for positive sums

These are the human proofs of Sliding window for positive sums, at full length.

The positive-sum sliding window returns the shortest window full

This proof shows: correctness.

We use the pseudocode of the page, on A[0..n−1]A[0..n-1] with A[i]≥0A[i] \ge 0 for all ii and s>0s > 0. Write S(i,j)=A[i]+⋯+A[j]S(i, j) = A[i] + \dots + A[j] for i≤ji \le j and S(i,i−1)=0S(i, i-1) = 0. For 0≤i≤n−10 \le i \le n - 1 let e(i)e(i) be the smallest jj with i≤j≤n−1i \le j \le n - 1 and S(i,j)≥sS(i, j) \ge s, or ∞\infty if there is none. Let D=min⁡i(e(i)−i+1)D = \min_i (e(i) - i + 1), with D=∞D = \infty if every e(i)e(i) is ∞\infty. The answer is DD, or 00 when D=∞D = \infty.

Lemma 1 (monotone sums). If i≤i′i \le i' and j′≤jj' \le j, then S(i′,j′)≤S(i,j)S(i', j') \le S(i, j).

Proof. S(i,j)−S(i′,j′)S(i, j) - S(i', j') is a sum of items, so it is not negative. ■\blacksquare

Lemma 2 (inner loop). Suppose that before line L4 of step rr we have t=S(l,r)t = S(l, r) and S(l,r−1)<sS(l, r - 1) < s, with l≤rl \le r. Then the inner loop stops. Each time line L5 runs, e(l)=re(l) = r for the current ll, and it records r−l+1=e(l)−l+1r - l + 1 = e(l) - l + 1. When the loop stops, t=S(l,r)<st = S(l, r) < s and l≤r+1l \le r + 1.

Proof. The loop keeps t=S(l,r)t = S(l, r), since line L6 removes A[l]A[l] and then moves ll. When l=r+1l = r + 1, t=S(r+1,r)=0<st = S(r + 1, r) = 0 < s, so the loop runs at most r−l+1r - l + 1 times. Each time the test t≥st \ge s passes with l≤rl \le r, we have S(l,r)≥sS(l, r) \ge s. Also S(l,r−1)<sS(l, r - 1) < s: for the first pass this is the hypothesis, and for a later pass with a larger ll it follows from Lemma 1 and the hypothesis on the first ll. So rr is the least end with sum at least ss from start ll, that is, e(l)=re(l) = r. ■\blacksquare

Claim. At the start of step rr, for 0≤r≤n0 \le r \le n (with step nn meaning the moment the outer loop ends):

  1. 0≤l≤r0 \le l \le r and t=S(l,r−1)<st = S(l, r - 1) < s;
  2. for every i<li < l, e(i)≤r−1e(i) \le r - 1 and best≤e(i)−i+1best \le e(i) - i + 1;
  3. if best<∞best < \infty, then best=e(i)−i+1best = e(i) - i + 1 for some i<li < l.

Proof. By induction on rr.

Base, r=0r = 0. Line L1 sets l=0l = 0, t=0=S(0,−1)<st = 0 = S(0, -1) < s and best=∞best = \infty. Parts 2 and 3 hold, because there is no i<0i < 0 and best=∞best = \infty.

Step. Assume the claim at the start of step r<nr < n. Line L3 sets t=S(l,r)t = S(l, r), and part 1 gives S(l,r−1)<sS(l, r - 1) < s. Let l0l_0 be ll at this moment and l1l_1 be ll after the inner loop. By Lemma 2 the inner loop stops with t=S(l1,r)<st = S(l_1, r) < s and l1≤r+1l_1 \le r + 1, so part 1 holds at the start of step r+1r + 1.

The starts ii with l0≤i<l1l_0 \le i < l_1 are those that the inner loop drops. For each of them Lemma 2 gives e(i)=re(i) = r, and line L5 makes best≤r−i+1=e(i)−i+1best \le r - i + 1 = e(i) - i + 1. For i<l0i < l_0, part 2 gave e(i)≤r−1e(i) \le r - 1 and best≤e(i)−i+1best \le e(i) - i + 1, and bestbest only decreases. So part 2 holds for every i<l1i < l_1 at the start of step r+1r + 1. Every value that line L5 writes is e(i)−i+1e(i) - i + 1 for a dropped start ii, which gives part 3. ■\blacksquare

Theorem. MIN-WINDOW returns DD if D<∞D < \infty, and 00 otherwise.

Proof. Apply the claim at r=nr = n. For i<li < l, part 2 gives best≤e(i)−i+1best \le e(i) - i + 1. For i≥li \ge l, Lemma 1 and part 1 give S(i,n−1)≤S(l,n−1)=t<sS(i, n - 1) \le S(l, n - 1) = t < s, so S(i,j)<sS(i, j) < s for every j≥ij \ge i and e(i)=∞e(i) = \infty. So best≤Dbest \le D. Part 3 gives best≥Dbest \ge D when bestbest is finite, and if best=∞best = \infty then no start has a finite e(i)e(i), by part 2 applied to i<li < l, so D=∞D = \infty. So best=Dbest = D, and lines L7 and L8 return the answer.

Termination and cost. The outer loop runs nn times. Line L6 increases ll, and l≤r+1≤nl \le r + 1 \le n by part 1, so line L6 runs at most nn times in all, and line L4 fails exactly nn times. The algorithm stops after O(n)O(n) operations. ■\blacksquare

Back to Sliding window for positive sums