Given an array of size N and an integer K, find the minimum for each and every contiguous subarray of size K.

**Examples**:

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

**Prerequisite**:

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.

The following approach is being used in the program:

- Pick first k elements and create a set of pair with these element and their index as described above.
- Now, use window sliding technique and Loop from j=0 to n-k:
- Get the minimum element from the set in the current window and print it.(The first element)
- 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.

**Why do we use a set of pairs instead of a set?**

A set doesn’t allow insertion of duplicate elements and a window of size k may have any number of duplicate elements. So, We insert a pair of the element and its index into the set.

Below is the implementation of the above approach:

`// CPP program to pint Minimum of ` `// all Subarrays using set in C++ STL ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to pint Minimum of ` `// all Subarrays using set in C++ STL ` `void` `minOfSubarrays(` `int` `arr[], ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Create a set of pairs ` ` ` `set<pair<` `int` `, ` `int` `> > q; ` ` ` ` ` `// Create an iterator to the set ` ` ` `set<pair<` `int` `, ` `int` `> >::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)); ` ` ` `} ` ` ` ` ` `for` `(` `int` `j = 0; j < n - k + 1; j++) { ` ` ` ` ` `// Iterator to the beginning of the ` ` ` `// set since it has the minimum value ` ` ` `it = q.begin(); ` ` ` ` ` `// Print the minimum element ` ` ` `// of current window ` ` ` `cout << 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)); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 5, 3, 4, 1, 1 }; ` ` ` ` ` `int` `K = 3; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `minOfSubarrays(arr, n, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3 1 1

**Time Complexity**: O(N * LogK)

## Recommended Posts:

- Maximum of all Subarrays of size k using set in C++ STL
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Sum of minimum and maximum elements of all subarrays of size k.
- Min difference between maximum and minimum element in all Y size subarrays
- Minimum cost required to convert all Subarrays of size K to a single element
- Sum of Bitwise-OR of all subarrays of a given Array | Set 2
- Find Maximum and Minimum element in a Set in C++ STL
- How to find the minimum and maximum element of a Vector using STL in C++?
- How to find the minimum and maximum element of an Array using STL in C++?
- All reverse permutations of an array using STL in C++
- Remove all consecutive duplicates from a string using STL in C++
- set::rbegin() and set::rend() in C++ STL
- set::begin() and set::end() in C++ STL
- Counting Inversions using Set in C++ STL
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Count smaller elements on right side using Set in C++ STL
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Binary Tree to Binary Search Tree Conversion using STL set
- Sum of all subarrays of size K
- Sliding Window Maximum (Maximum 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.