Sliding Window
Think of it like this
You're looking out of a moving train window. As the train moves, new scenery enters from the right and old scenery leaves from the left. At every moment you only care about what's in the window right now, not all the scenery from the beginning of the journey.
The sliding window pattern does exactly this for arrays and strings: maintain a running aggregate (sum, count, max) for a subarray, and update it by adding the new right element and removing the old left element, instead of recomputing from scratch.
arr = [2, 4, 7, 1, 9, 3, 6], window size k = 3
Naive O(nยทk): [2,4,7]=13 [4,7,1]=12 [7,1,9]=17 [1,9,3]=13 [9,3,6]=18
Sliding O(n): start=2+4+7=13
step: 13 - 2(left) + 1(right) = 12
step: 12 - 4 + 9 = 17
step: 17 - 7 + 3 = 13
step: 13 - 1 + 6 = 18
Same answers. But each step is O(1) instead of O(k). Total: O(n).