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:
- Traverse the given array upto K elements and store the count of each element into a map.
- 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.
- Repeat the above steps for each subarray formed.
Below is the implementation of the above approach:
// 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 starting // of the Map auto itMax = Map.end(); itMax--; // Iterator points to end 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; } |
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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Sum of minimum and maximum elements of all subarrays of size k.
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Maximum of all Subarrays of size k using set in C++ STL
- Maximum sum two non-overlapping subarrays of given size
- Maximum of all subarrays of size K using Segment Tree
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Number of subarrays whose minimum and maximum are same
- Split array into two subarrays such that difference of their maximum is minimum
- Minimum partitions of maximum size 2 and sum limited by given value
- Find maximum (or minimum) sum of a subarray of size k
- Product of all Subsequences of size K except the minimum and maximum Elements
- Minimum size Subarray with maximum sum in non-increasing order
- Sum of all subarrays of size K
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.

