A sensor emits n non-negative integer samples one at a time. You maintain a sliding window of the most recent k samples (fewer than k while the stream is still filling up). After receiving each sample, output the floor of the average of all samples currently held in the window.
Concretely, after receiving sample number t (1-indexed), the window holds the last min(t, k) samples; output floor(sum_of_window / size_of_window). Because all samples are non-negative, this floor equals integer division.
Line 1: two integers n and k.
Line 2: n space-separated non-negative integers, the samples in arrival order.
One line with n space-separated integers: after each sample, the floor of the current window average.
Example 1
Input
5 3 1 2 3 4 5
Expected
1 1 2 3 4
Explanation
Windows and floored averages: [1]->1; [1,2]->1 (3//2); [1,2,3]->2; [2,3,4]->3; [3,4,5]->4.
Example 2
Input
3 5 4 4 4
Expected
4 4 4
Explanation
k exceeds the stream length, so the window is every sample seen so far: [4]->4; [4,4]->4; [4,4,4]->4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →