Minimum and Maximum of all subarrays of size K using Map

Given an array arr[] of N integers and an integer K, the task is to find the minimum and maximum of all subarrays of size K.

Examples:

Input: arr[] = {2, -2, 3, -9, -5, -8}, K = 4
Output:
-9 3
-9 3
-9 3
Explanation:
Below are the subarray of size 4 and minimum and maximum value of each subarray:
1. {2, -2, 3, -9}, minValue = -9, maxValue = 3
2. {-2, 3, -9, -5}, minValue = -9, maxValue = 3
3. {3, -9, -5, -8}, minValue = -9, maxValue = 3

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

Approach:



  1. Traverse the given array upto K elements and store the count of each element into a map.
  2. After inserting K elements, for each remaining elements do the following:
    • Increase the frequency of current element arr[i] by 1.
    • Decrease the frequency of arr[i – K + 1] by 1 to store the frequency of current subarray(arr[i – K + 1, i]) of size K.
    • Since map stores the key value pair in sorted order. Therefore the iterator at the starting of the map stores the minimum element and at the ending of the map stores the maximum element. Print the minimum and maximum element of the current subarray.
  3. Repeat the above steps for each subarray formed.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the minimum and
// maximum element for each subarray
// of size K
int maxSubarray(int arr[], int n, int k)
{
  
    // To store the frequency of element
    // for every subarray
    map<int, int> Map;
  
    // To count the subarray array size
    // while traversing array
    int l = 0;
  
    // Traverse the array
    for (int i = 0; i < n; i++) {
  
        // Increment till we store the
        // frequency of first K element
        l++;
  
        // Update the count for current
        // element
        Map[arr[i]]++;
  
        // If subarray size is K, then
        // find the minimum and maximum
        // for each subarray
        if (l == k) {
  
            // Iterator points to end
            // of the Map
            auto itMax = Map.end();
            itMax--;
  
            // Iterator points to start of
            // the Map
            auto itMin = Map.begin();
  
            // Print the minimum and maximum
            // element of current sub-array
            cout << itMin->first << ' '
                 << itMax->first << endl;
  
            // Decrement the frequency of
            // arr[i - K + 1]
            Map[arr[i - k + 1]]--;
  
            // if arr[i - K + 1] is zero
            // remove from the map
            if (Map[arr[i - k + 1]] == 0) {
                Map.erase(arr[i - k + 1]);
            }
  
            l--;
        }
    }
    return 0;
}
  
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 5, 4, 3, 2, 1, 6,
                  3, 5, 4, 2, 1 };
  
    // Subarray size
    int k = 3;
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    maxSubarray(arr, n, k);
    return 0;
}

chevron_right


Output:

3 5
2 4
1 3
1 6
1 6
3 6
3 5
2 5
1 4

Time Complexity: O(N*log K), where N is the number of element.
Auxiliary Space: O(K), where K is the size of subarray.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.