An assembly line records an integer load reading for each of n consecutive minutes. For a fixed window length k, consider every contiguous window of exactly k consecutive minutes (there are n - k + 1 of them, sliding by one minute each time). For each window, its "spike" is the MAXIMUM load reading inside that window.
Print the SUM of the spikes over all windows.
Line 1: two integers n and k.
Line 2: n space-separated integers, the load readings.
A single integer: the sum of the per-window maxima.
Example 1
Input
5 3 4 2 9 1 5
Expected
27
Explanation
Every size-3 window ([4,2,9], [2,9,1], [9,1,5]) happens to contain the value 9, so each window's maximum is 9, giving a total of 27.
Example 2
Input
4 2 -3 -1 -7 -2
Expected
-4
Explanation
With all-negative readings, each window's maximum is its least negative value: -1 (from [-3,-1]), -1 (from [-1,-7]) and -2 (from [-7,-2]), summing to -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 →