Some problems ask us to look at a contiguous sub-part of an array. Instead of re-calculating everything, we can 'slide' a window over the data.
Let's find the max sum of any '3' consecutive numbers. We start by building the first window of size 3.
To move right, we DON'T need to sum 1+5+1 from scratch. We simply subtract the element the left LEFT (2) and add the element entering RIGHT (1).
8 - 2 + 1 = 7. The new sum is 7. Is it higher than our Max (8)? No.
Move right again. Subtract 1. Add 3.
Slide right. Subtract 5. Add 2. Sum: 6. Max stays 9.
Slide right. Subtract 1. Add 9. Sum = 14! New Record.
Slide right. Subtract 3. Add 8. Sum = 19! Even better.
Last slide. Subtract 2. Add 3. Sum = 20. The winner is 20.
We touched each element only twice (once entering, once leaving). This is O(n), much faster than checking every subarray separately.
Sliding Window turns nested loops O(n*k) into linear time O(n).