Count Max Element in WindowEasy

Count Max Element in Window

EasyPractice library · +10 XPsliding windowarrays

Given an array of integers A of size N and an integer K, find the maximum element in each subarray of size K. Print these maximums for all possible windows.

Input

The first line contains two integers N and K separated by a space. The second line contains N integers representing the array A.

Output

Print N - K + 1 integers, separated by spaces, where the i-th integer is the maximum of the subarray A[i...i+K-1].

Constraints

1 <= K <= N <= 1000. Each element of A is between -1000 and 1000.

Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests

Example 1

Input
5 3
1 3 -1 -3 5
Output
3 3 5

The windows are [1, 3, -1] (max 3), [3, -1, -3] (max 3), [-1, -3, 5] (max 5). Output: 3 3 5

Example 2

Input
7 2
1 2 3 4 5 6 7
Output
2 3 4 5 6 7

The windows are [1, 2] (max 2), [2, 3] (max 3), ..., [6, 7] (max 7). Output: 2 3 4 5 6 7

Hints