A turbine emits n vibration readings in order. To flag instability, a monitor slides a window of exactly k consecutive readings and, for each window position, computes the range: the maximum reading minus the minimum reading within that window.
Output the range of every contiguous window of size k, from the leftmost window to the rightmost.
Line 1: two integers n and k.
Line 2: n space-separated integers, the vibration readings.
One line with n - k + 1 space-separated integers: the range (max minus min) of each window, left to right.
Example 1
Input
5 3 1 3 2 5 4
Expected
2 3 3
Explanation
Windows: [1,3,2] range 3-1=2; [3,2,5] range 5-2=3; [2,5,4] range 5-2=3.
Example 2
Input
3 3 6 6 6
Expected
0
Explanation
The only window has equal readings, so its max and min match and the range is 0.
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 →