MetaOA #23
Sliding Window Average
Estimated EasyJudge-ready
Topics (AI-labelled)Two PointersPrefix & Ranges
Given an integer array and a positive window length, output the average of every contiguous window; if the window length exceeds the array length, the result is empty.
Input
This site's input: the first line contains n and w (0≤n≤50000, 1≤w≤100000), and the second line contains n integers (−10⁹..10⁹).
Output
The first line is the number of results. If it is greater than 0, the second line gives the averages in window order, separated by spaces; for an empty result, output only 0. Each value may have an absolute or relative error of at most 10⁻⁵, and NaN or Infinity must not be output.
Example 1
Input:
9 7
1 2 3 4 5 6 7 8 9
Output:
3
4 5 6
Example 2
Input:
3 3
1 0 0
Output:
1
0.33333333333333333333333333333333333333333333333333
Example 3
Input:
2 3
1 2
Output:
0
Example notes
Example 1: the three windows sum to 28, 35, and 42; dividing by 7 gives 4.000000, 5.000000, and 6.000000.
Example 2: the only window's average is 1/3, which is 0.333333 to six decimal places.
Example 3: the window length 3 exceeds the array length 2, so there is no complete window, and the output count is 0.
Editorial
Loading solution…