# Median of sliding window in an array | Set 2

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 = 3
Output: 5 8 8 3 3 3
Explanation:
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 = 4
Output: 6.5 6.5 5.5 3.0 2.5

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

1. 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.
2. The middle element can be obtained by the find_by_order() method in O(logN) computational complexiy.
3. 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.
4. 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 ` `#include ` ` `  `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); ` `    ``findMedian(arr, n, k); ` ` `  `    ``return` `0; ` `} `

Output:

```5 8 8 3 3 3
```

Time Complexity: O(NlogK)
Auxiliary Space: O(K) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.