For a fixed window of size :
Build the initial window with the first elements.
Slide the window: add the next element, remove the first element.
Update your answer at each position. Each element enters once and leaves once. Total: . This pattern works for maximum sum of size , average of size , or any aggregate over fixed-size subarrays.