Thursday, February 24, 2011

Given an array and an integer k, find the maximum for each and every contiguous sub array of size k.

Sample Input :
1 2 3 1 4 5 2 3 6
3 [ value of k ]

Sample Output :
3
3
4
5
5
5
6



#include

using namespace std;

int max(int a[],int r,int pos)
{
int m=a[pos];
int i=0;
for(i=pos+1;i {
if(a[i]>m)
{
m=a[i];
}

}

return m;
}
void maxi(int a[],int r,int pos)
{
int m;

if(pos > 10-r ) return ;
m=max(a,3,pos);
cout< maxi(a,r,pos+1);
}
int main()
{
int a[]={1, 2, 3 ,1 ,4 ,5 ,2 ,3 ,6};

int pos=0;

maxi(a,3,pos);


return 1;
}

No comments :