Sunday, November 19, 2017

Sliding Window Maximum

Given a list of N positive integers and a sliding window of size W, the program must print the compare the maximum value CURRMAX present in the specific window instance with that of the previous window maximum PREVMAX and print the maximum value of CURRMAX and PREVMAX. The maximum values must be separated by a space.

Input Format:
The first line contains N and W separated by a space. The second line contains N integer values separated by a space.

Output Format:
The first line contains the maximum values separated by a space.

Boundary Conditions: 1 <= N <= 10000 W <= N

Example Input/Output 1:
Input:
7 3
10 3 8 4 2 11 1
Output:
10 10 8 11 11

Solution:
n,x=[int(i) for i in input().split()]
a,p,c=[int(i) for i in input().split()],0,0
for i in range(n-x+1):
    c=max(a[i:i+x])
    print(max(c,p),end=' ')
    p=c
Share: