easySliding WindowPure DSA~13 min
Max Average Rating Window
A reviews dashboard surfaces the best stretch of k consecutive days by total rating. Because the window length is fixed, the highest total also gives the highest average — find that maximum window sum.
Problem
Given an array ratings and an integer k, return the maximum sum of any contiguous window of exactly k elements.
Input
An array ratings of length n (1 ≤ k ≤ n ≤ 10^5), values in [-10^4, 10^4].
Output
A single integer — the maximum window sum.
Constraints
- 1 ≤ k ≤ n ≤ 100,000
- Values may be negative
- Window length is exactly k
Examples
Example 1
Input
ratings = [1, 12, -5, -6, 50, 3], k = 4
Output
51
The window [12, -5, -6, 50] sums to 51, the largest among all length-4 windows.
Example 2
Input
ratings = [5], k = 1
Output
5
Only one window exists.