Sunday, 26 May 2019

Maximum in sub-arrays of length K:

Given an array of integers and a number k, where 1 <= k <= length of the array, compute the maximum values of each subarray of length k.

For example, given array = [10, 5, 2, 7, 8, 7] and k = 3, we should get: [10, 7, 8, 8], since:

10 because {10,5,2} (10 is maximum in sub array)
7   because {5,2,7} (7 is maximum in sub array)
8   because {2,7,8} (8 is maximum in sub array)
8   because {7,8,7} (8 is maximum in sub array)

Imput :
first line of  input contains integer N,
second line of input contains array of integers of length N;
third line of input contains integer K,the length of sub array

Output:
otuput consists of an array of maximum numbers of each subarray.

Program Code:

#include <stdio.h>
int main()
{
    int k,n,a[100],ind=0;
    int i,j,max[100],h,m;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    scanf("%d",&k);
    h=k;
    for(i=0;i<=n-k;i++)
    {
        
        m=a[i];
for(j=i+1;j<h;j++)
        {
if(a[j]>m)
            {
m=a[j];
            }
        }
        max[i]=m;
        h++;
    }
    
    for(i=0;i<=n-k;i++)
    {
printf("%d ",max[i]);
    }

    return 0;
}

1 comment:

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...