Monday, 6 August 2012

SUBARRAYS


SPOJ Problem Set (classical)

10582. subarrays

Problem code: ARRAYSUB

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

Input

the number n denoting number of elements in the array then after a new line we have the numbers of the array
and then k in a new line
n<10 p="p"> k<10 p="p"> 1 <= k <=n
and each element of the array is between 0 and 10^6

Output

print the output array

Example

Input:
9
1  2  3  1  4  5  2  3  6
3
Output:
3 3 4 5 5 5 6









SOLUTION------------------------------------------------------------------------------------------

#include
int main()
{
         long long int n,k,i,j,max;
        scanf("%lld",&n);
        long long int arr[n];
        for(i=0;i        {
                scanf("%lld",&arr[i]);
        }
        scanf("%lld",&k);
        max=arr[0];
        for(i=0;i        {
                if(i==0 || max==arr[i-1]){
                max=arr[i];
                for(j=i;j                        if(arr[j]>max)
                                max=arr[j];
                }
                }
                else{
                        if(arr[i+k-1]>max)
                                max=arr[i+k-1];
                }
               
                printf("%lld ",max);
                if(i+k>=n){
                        break;
                }
        }
        return 0;
}

No comments: