Skip to content
Related Articles
Minimum and Maximum of all subarrays of size K using Map
• Last Updated : 25 Jun, 2020

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

 `// C++ program for the above approach`` ` `#include ``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);`` ` `    ``// Function Call``    ``maxSubarray(arr, n, k);``    ``return` `0;``}`
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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up