You are given an array of n integers and a window length k. A window is a block of k consecutive elements. Sliding the window from the left edge to the right edge produces exactly n - k + 1 windows. For each window, report the largest element it contains.
Output the window maxima in order, from the leftmost window to the rightmost.
Line 1: two integers n and k separated by a space.
Line 2: n space-separated integers, the array values.
A single line with the n - k + 1 window maxima, separated by single spaces, in left-to-right order.
Example 1
Input
8 3 1 3 -1 -3 5 3 6 7
Expected
3 3 5 5 6 7
Explanation
The six windows of length 3 are [1,3,-1]->3, [3,-1,-3]->3, [-1,-3,5]->5, [-3,5,3]->5, [5,3,6]->6, [3,6,7]->7, giving 3 3 5 5 6 7.
Example 2
Input
4 2 4 4 4 4
Expected
4 4 4
Explanation
Every window of length 2 is [4,4], so each maximum is 4, giving 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 →