Maximum Of K- size subarrays (Deque)

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

Example:

Input:  n = 9, k = 3, a ={1, 2, 3, 1, 4, 5, 2, 3, 6}
Output: 3 3 4 5 5 5 6

Approach

C++

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long n = 9, k = 3;

    long long a[n] = {1, 2, 3, 1, 4, 5, 2, 3, 6};
    for (long long i = 0; i <= n - k; i++)
        cout << *max_element(a + i, a + i + k) << " ";
    cout << "\n";
}


No comments:

Post a Comment