Maximum of all Subarrays of size k using set in C++ STL (original) (raw)
Last Updated : 23 Nov, 2022
Given an array of size N and an integer K, the task is to find the maximum for each and every contiguous sub-array of size K and print the sum of all these values in the end. Examples:
Input: arr[] = {4, 10, 54, 11, 8, 7, 9}, K = 3
Output: 182Input: arr[] = {1, 2, 3, 4, 1, 6, 7, 8, 2, 1}, K = 4
Output: 45
Prerequisite:
Approach: Set performs insertion and removal operation in O(logK) time and always stores the keys in the sorted order. The idea is to use a set of pairs where the first item in the pair is the element itself and the second item in the pair contains the array index of the element.
- Pick first k elements and create a set of pair with these element and their index as described above.
- Now, set sum = 0 and use window sliding technique and Loop from j = 0 to n - k:
- Get the maximum element from the set (the last element) in the current window and update sum = sum + currMax.
- Search for the leftmost element of current window in the set and remove it.
- Insert the next element of the current window in the set to move to next window.
Below is the implementation of the above approach:
C++ `
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;
// Function to return the sum of maximum of // all k size sub-arrays using set in C++ STL int maxOfSubarrays(int arr[], int n, int k) { // Create a set of pairs set<pair<int, int> > q;
// Create a reverse iterator to the set
set<pair<int, int> >::reverse_iterator it;
// Insert the first k elements along
// with their indices into the set
for (int i = 0; i < k; i++) {
q.insert(pair<int, int>(arr[i], i));
}
// To store the sum
int sum = 0;
for (int j = 0; j < n - k + 1; j++) {
// Iterator to the end of the
// set since it has the maximum value
it = q.rbegin();
// Add the maximum element
// of the current window
sum += it->first;
// Delete arr[j] (Leftmost element of
// current window) from the set
q.erase(pair<int, int>(arr[j], j));
// Insert next element
q.insert(pair<int, int>(arr[j + k], j + k));
}
// Return the required sum
return sum;
}
// Driver Code int main() { int arr[] = { 4, 10, 54, 11, 8, 7, 9 };
int K = 3;
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxOfSubarrays(arr, n, K);
return 0;
}
`
Complexity Analysis:
- Time Complexity: O(n Log n)
- Auxiliary Space : O(k)
The above problem can be solved in O(n) time. Please see below Dequeue based solution for the same.
Sliding Window Maximum (Maximum of all subarrays of size k)
Similar Reads
- Maximum of all subarrays of size K using Segment Tree Given an array arr[] and an integer K, the task is to 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 Explanation: Maximum of 1, 2, 3 is 3 Maximum of 2, 3, 1 is 3 Maximum of 3, 1, 4 is 4 Maximum of 1 14 min read
- 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. 9 min read
- Sliding Window Maximum (Maximum of all subarrays of size K) Given an array arr[] of integers and an integer k, your task is to find the maximum value for each contiguous subarray of size k. The output should be an array of maximum values corresponding to each contiguous subarray.Examples : Input: arr[] = [1, 2, 3, 1, 4, 5, 2, 3, 6], k = 3Output: [3, 3, 4, 5, 15+ min read
- Find maximum (or minimum) sum of a subarray of size k Given an array of integers and a number k, find the maximum sum of a subarray of size k. Examples: Input : arr[] = {100, 200, 300, 400}, k = 2Output : 700Input : arr[] = {1, 4, 2, 10, 23, 3, 1, 0, 20}, k = 4 Output : 39Explanation: We get maximum sum by adding subarray {4, 2, 10, 23} of size 4.Input 14 min read
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time Given an array arr[] of N integers and another integer k ? N. The task is to find the maximum element of every sub-array of size k. Examples: Input: arr[] = {9, 7, 2, 4, 6, 8, 2, 1, 5} k = 3 Output: 9 7 6 8 8 8 5 Explanation: Window 1: {9, 7, 2}, max = 9 Window 2: {7, 2, 4}, max = 7 Window 3: {2, 4, 8 min read
- Sum of minimum and maximum elements of all subarrays of size k. Given an array of both positive and negative integers, the task is to compute sum of minimum and maximum elements of all sub-array of size k.Examples: Input : arr[] = {2, 5, -1, 7, -3, -1, -2} K = 4Output : 18Explanation : Subarrays of size 4 are : {2, 5, -1, 7}, min + max = -1 + 7 = 6 {5, -1, 7, -3 15+ min read
- Maximum even numbers present in any subarray of size K Given an array arr[] of size N and an integer K, the task is to find the maximum number of even numbers present in any subarray of size K. Examples: Input: arr[] = {2, 3, 5, 4, 7, 6}, K = 3 Output: 2 Explanation: Subarrays of size K(=3) with maximum count of even numbers are { arr[3], arr[4], arr[5] 12 min read
- Maximum Unique Element in every subarray of size K Given an array and an integer K. We need to find the maximum of every segment of length K which has no duplicates in that segment. Examples: Input : a[] = {1, 2, 2, 3, 3}, K = 3. Output : 1 3 2 For segment (1, 2, 2), Maximum = 1. For segment (2, 2, 3), Maximum = 3. For segment (2, 3, 3), Maximum = 2 7 min read
- Maximum sum subarray of size range [L, R] Given an integer array arr[] of size N and two integer L and R. The task is to find the maximum sum subarray of size between L and R (both inclusive). Example: Input: arr[] = {1, 2, 2, 1}, L = 1, R = 3 Output: 5 Explanation: Subarray of size 1 are {1}, {2}, {2}, {1} and maximum sum subarray = 2 for 8 min read
- Maximum number of set bits count in a K-size substring of a Binary String Given a binary string S of size N and an integer K. The task is to find the maximum number of set bit appears in a substring of size K. Examples: Input: S = "100111010", K = 3 Output: 3 Explanation: The substring "111" contains 3 set bits. Input:S = "0000000", K = 4 Output: 0 Explanation: S doesn't 10 min read