**Prerequisites:** Policy based data structure, Sliding window technique.

Given an array of integer **arr[]** and an integer **K**, the task is to find the median of each window of size **K** starting from the left and moving towards the right by one position each time.

**Examples:**

Input:arr[] = {-1, 5, 13, 8, 2, 3, 3, 1}, K = 3Output:5 8 8 3 3 3Explanation:

1st Window: {-1, 5, 13} Median = 5

2nd Window: {5, 13, 8} Median = 8

3rd Window: {13, 8, 2} Median = 8

4th Window: {8, 2, 3} Median = 3

5th Window: {2, 3, 3} Median = 3

6th Window: {3, 3, 1} Median = 3

Input:arr[] = {-1, 5, 13, 8, 2, 3, 3, 1}, K = 4Output:6.5 6.5 5.5 3.0 2.5

**Naive Approach:**

The simplest approach to solve the problem is to traverse over every window of size **K** and sort the elements of the window and find the middle element. Print the middle element of every window as the median.**Time Complexity:** O(N*KlogK)**Auxiliary Space:** O(K)

**Sorted Set Approach:** Refer to Median of sliding window in an array to solve the problem using SortedSet.

**Ordered Set Approach:**

In this article, an approach to solving the problem using a Policy-based Ordered set data structure.

Follow the steps below to solve the problem:

- Insert the first window of size
**K**in the Ordered_set( maintains a sorted order). Hence, the middle element of this Ordered set is the required median of the corresponding window. - The middle element can be obtained by the find_by_order() method in
**O(logN)**computational complexiy. - Proceed to the following windows by remove the first element of the previous window and insert the new element. To remove any element from the set, find the order of the element in the
**Ordered_Set**using**order_by_key()**, which fetches the result in O(logN) computational complexity, and**erase()**that element by searching its obtained order in the Ordered_Set using**find_by_order()**method. Now add the new element for the new window. - Repeat the above steps for each window and print the respective medians.

Below is the implementation of the above approach.

## CPP

`// C++ Program to implement the` `// above approach` `#include <bits/stdc++.h>` `#include <ext/pb_ds/assoc_container.hpp>` ` ` `using` `namespace` `std;` `using` `namespace` `__gnu_pbds;` ` ` `// Policy based data structure` `typedef` `tree<` `int` `, null_type,` ` ` `less_equal<` `int` `>, rb_tree_tag,` ` ` `tree_order_statistics_node_update>` ` ` `Ordered_set;` ` ` `// Function to find and return the` `// median of every window of size k` `void` `findMedian(` `int` `arr[], ` `int` `n,` ` ` `int` `k)` `{` ` ` ` ` `Ordered_set s;` ` ` ` ` `for` `(` `int` `i = 0; i < k; i++)` ` ` `s.insert(arr[i]);` ` ` ` ` `if` `(k & 1) {` ` ` ` ` `// Value at index k/2` ` ` `// in sorted list.` ` ` `int` `ans = *s.find_by_order(k / 2);` ` ` ` ` `cout << ans << ` `" "` `;` ` ` ` ` `for` `(` `int` `i = 0; i < n - k; i++) {` ` ` ` ` `// Erasing Element out of window.` ` ` `s.erase(s.find_by_order(` ` ` `s.order_of_key(` ` ` `arr[i])));` ` ` ` ` `// Inserting newer element` ` ` `// to the window` ` ` `s.insert(arr[i + k]);` ` ` ` ` `// Value at index k/2 in` ` ` `// sorted list.` ` ` `ans = *s.find_by_order(k / 2);` ` ` ` ` `cout << ans << ` `" "` `;` ` ` `}` ` ` `cout << endl;` ` ` `}` ` ` `else` `{` ` ` ` ` `// Getting the two middle` ` ` `// median of sorted list.` ` ` `float` `ans = ((` `float` `)*s.find_by_order(` ` ` `(k + 1) / 2 - 1)` ` ` `+ (` `float` `)*s.find_by_order(k` ` ` `/ 2))` ` ` `/ 2;` ` ` ` ` `printf` `(` `"%.2f "` `, ans);` ` ` ` ` `for` `(` `int` `i = 0; i < n - k; i++) {` ` ` `s.erase(s.find_by_order(` ` ` `s.order_of_key(arr[i])));` ` ` ` ` `s.insert(arr[i + k]);` ` ` ` ` `ans = ((` `float` `)*s.find_by_order(` ` ` `(k + 1) / 2 - 1)` ` ` `+ (` `float` `)*s.find_by_order(k` ` ` `/ 2))` ` ` `/ 2;` ` ` ` ` `printf` `(` `"%.2f "` `, ans);` ` ` `}` ` ` `cout << endl;` ` ` `}` `}` ` ` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { -1, 5, 13, 8, 2,` ` ` `3, 3, 1 };` ` ` `int` `k = 3;` ` ` ` ` `int` `n = ` `sizeof` `(arr)` ` ` `/ ` `sizeof` `(arr[0]);` ` ` `findMedian(arr, n, k);` ` ` ` ` `return` `0;` `}` |

**Output:**

5 8 8 3 3 3

**Time Complexity:** O(NlogK)**Auxiliary Space:** O(K)

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 **Geeks Classes Live** and **Geeks Classes Live USA**