Maximum of all subarrays of size k
PROBLEM :
Given an array and an integer k, find the maximum for each and every contiguous subarray of size k.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer 'N' denoting the size of array and the size of subarray 'k'.
The second line contains 'N' space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print the maximum for every subarray of size k.
Constraints:
1 = T = 100
1 = N = 100
1 = k = N
0 =A[i]<1000
Example:
Input:
2
9 3
1 2 3 1 4 5 2 3 6
10 4
8 5 10 7 9 4 15 12 90 13
Output:
3 3 4 5 5 5 6
10 10 10 15 15 90 90
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void Maximum_of_all_subarrays(int [],int ,int ) ;
int max_of(int ,int ) ;
int main()
{
int t,no,k,arr[100],i ;
cin>>t ;
while(t--)
{
cin>>no>>k ;
for(i=0;i<no;i++)
cin>>arr[i] ;
Maximum_of_all_subarrays(arr,no,k) ;
cout<<endl ;
}
return 0;
}
void Maximum_of_all_subarrays(int arr[],int no,int k)
{
int i,max,check ;
int temp1[no],temp2[no] ;
check=1 ;
max=arr[0] ;
for(i=0;i<no;i++)
{
if(check>k)
{
check=1 ;
max=arr[i] ;
}
if(arr[i]>max)
max=arr[i] ;
temp1[i]=max ;
check++ ;
}
max=arr[no-1] ;
for(i=no-1;i>=no-no%k;i--)
{
if(arr[i]>max)
max=arr[i] ;
temp2[i]=max ;
}
check=1 ;
max=arr[i] ;
for(i;i>=0;i--)
{
if(check>k)
{
check=1 ;
max=arr[i] ;
}
if(arr[i]>max)
max=arr[i] ;
temp2[i]=max ;
check++ ;
}
for(i=0;i<no-k+1;i++)
cout<<max_of(temp1[i+k-1],temp2[i])<<" " ;
}
int max_of(int a,int b)
{
return a>b?a:b ;
}
---------------------------------------------------------------------------------
Given an array and an integer k, find the maximum for each and every contiguous subarray of size k.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer 'N' denoting the size of array and the size of subarray 'k'.
The second line contains 'N' space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print the maximum for every subarray of size k.
Constraints:
1 = T = 100
1 = N = 100
1 = k = N
0 =A[i]<1000
Example:
Input:
2
9 3
1 2 3 1 4 5 2 3 6
10 4
8 5 10 7 9 4 15 12 90 13
Output:
3 3 4 5 5 5 6
10 10 10 15 15 90 90
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void Maximum_of_all_subarrays(int [],int ,int ) ;
int max_of(int ,int ) ;
int main()
{
int t,no,k,arr[100],i ;
cin>>t ;
while(t--)
{
cin>>no>>k ;
for(i=0;i<no;i++)
cin>>arr[i] ;
Maximum_of_all_subarrays(arr,no,k) ;
cout<<endl ;
}
return 0;
}
void Maximum_of_all_subarrays(int arr[],int no,int k)
{
int i,max,check ;
int temp1[no],temp2[no] ;
check=1 ;
max=arr[0] ;
for(i=0;i<no;i++)
{
if(check>k)
{
check=1 ;
max=arr[i] ;
}
if(arr[i]>max)
max=arr[i] ;
temp1[i]=max ;
check++ ;
}
max=arr[no-1] ;
for(i=no-1;i>=no-no%k;i--)
{
if(arr[i]>max)
max=arr[i] ;
temp2[i]=max ;
}
check=1 ;
max=arr[i] ;
for(i;i>=0;i--)
{
if(check>k)
{
check=1 ;
max=arr[i] ;
}
if(arr[i]>max)
max=arr[i] ;
temp2[i]=max ;
check++ ;
}
for(i=0;i<no-k+1;i++)
cout<<max_of(temp1[i+k-1],temp2[i])<<" " ;
}
int max_of(int a,int b)
{
return a>b?a:b ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment