Find the shortest run of values whose sum reaches 7. Grow the right end until the sum is enough, then shrink the left end while it still is.
Finding the shortest contiguous run with a sum of at least 7.
Grow right to index 0. 2 joins, so the sum is 2. Still short of 7.
The right end moves to index 0 and the sum becomes 2. That is still below the target.
Grow right to index 1. 3 joins, so the sum is 5. Still short of 7.
The right end moves to index 1 and the sum becomes 5. That is still below the target.
Grow right to index 2. 1 joins, so the sum is 6. Still short of 7.
The right end moves to index 2 and the sum becomes 6. That is still below the target.
Grow right to index 3. 2 joins, so the sum is 8. That reaches 7, so the left end can start shrinking.
The right end moves to index 3 and the sum becomes 8. That reaches the target.
Length 4 is the best so far. Drop 2 from the left, leaving 6. Now short of 7, so grow right next.
The left end moves to index 1, dropping 2. The sum is 6. It no longer reaches the target.
Grow right to index 4. 4 joins, so the sum is 10. That reaches 7, so the left end can start shrinking.
The right end moves to index 4 and the sum becomes 10. That reaches the target.
Length 4 is no better than 4. Drop 3 from the left, leaving 7. Still enough, so shrink again.
The left end moves to index 2, dropping 3. The sum is 7. It still reaches the target.
Length 3 is the best so far. Drop 1 from the left, leaving 6. Now short of 7, so grow right next.
The left end moves to index 3, dropping 1. The sum is 6. It no longer reaches the target.
Grow right to index 5. 3 joins, so the sum is 9. That reaches 7, so the left end can start shrinking.
The right end moves to index 5 and the sum becomes 9. That reaches the target.
Length 3 is no better than 3. Drop 2 from the left, leaving 7. Still enough, so shrink again.
The left end moves to index 4, dropping 2. The sum is 7. It still reaches the target.
Length 2 is the best so far. Drop 4 from the left, leaving 3. Now short of 7, so grow right next.
The left end moves to index 5, dropping 4. The sum is 3. It no longer reaches the target.
The shortest run is indices 4 to 5, length 2. Each end moved only forward, 11 moves in all, so O(n).
The shortest run with sum at least 7 has length 2.