0

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

Examples:

Input :
arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6}
k = 3
Output :
3 3 4 5 5 5 6

My code:

void printKMax(int arr[], int n, int k)
{
    int j, max;

    for (int i = 0; i <= n-k; i++)
    {
        max = arr[i];

        for (j = 1; j < k; j++)
        {
            if (arr[i+j] > max)
               max = arr[i+j];
        }
        printf("%d ", max);
    }
}

Time Complexity O(n^2) If there is some linear solution possible i.e of O(n) time complexity by using BIT Tree or using any other data structure and how can i expand my algorithm to find the no. of increasing sequence also.

QWE
  • 45
  • 7

0 Answers0