# Maximum of all Subarrays of size k using set in C++ STL

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:182

Input: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.

- Get the maximum element from the set (the last element) in the current window and update

Below is the implementation of the above approach:

`// 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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

182

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)

## Recommended Posts:

- 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)
- Minimum and Maximum of all subarrays of size K using Map
- Maximum sum two non-overlapping subarrays of given size
- Minimise the maximum element of N subarrays of size K
- Sum of minimum and maximum elements of all subarrays of size k.
- Maximum of all subarrays of size K using Segment Tree
- Maximum absolute difference between sum of subarrays of size K
- Sum of all subarrays of size K
- Max sum of M non-overlapping subarrays of size K
- Find an array of size N having exactly K subarrays with sum S
- Counting inversions in all subarrays of given size
- Count of subarrays of size K with elements having even frequencies
- Count of subarrays of size K which is a permutation of numbers from 1 to K
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Maximum size of square such that all submatrices of that size have sum less than K
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Minimum count of increment of K size subarrays required to form a given Array

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.