Wednesday 22 June 2016

Given a sliding window of size k print the maximum of the numbers under the sliding window. Example: Consider a sliding window of size k equals 3. Let the array be [3,2,7,6,5,1,2,3,4] the output should print 7 as the first output as first window contains {3,2,7} and second window contains {2,7,6} and so on and the final output is {7,7,7,6,5,3,4}

#include<stdio.h>
#include<string.h>
int max(int ar[],int b,int k1){
int m=0,l;
for(l=b;k1!=0;l++,k1--){
if(m<=ar[l]){
m=ar[l];
}
}
return m;
}
int main(){
int n,k,i,j,len,a[100];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);

scanf("%d",&k);
for(i=0;i+k<=n;i++){
len=max(a,i,k);
printf("%d ",len);
}
return 0;
}

No comments:

Post a Comment