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 = 3Input: arr[] = {-1, 5, 13, 8, 2, 3, 3, 1}, K = 4
Output: 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; } |
5 8 8 3 3 3
Time Complexity: O(NlogK)
Auxiliary Space: O(K)
Recommended Posts:
- Median of sliding window in an array
- Sliding Window Maximum : Set 2
- Window Sliding Technique
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Maximize the median of the given array after adding K elements to the same array
- Maximize the median of an array
- Program for Mean and median of an unsorted array
- Maximize median after doing K addition operation on the Array
- Find the median array for Binary tree
- Change in Median of given array after deleting given elements
- Finding Median of unsorted Array in linear time using C++ STL
- Median of an unsorted array using Quick Select Algorithm
- Count distinct median possible for an Array using given ranges of elements
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Find K elements whose absolute difference with median of array is maximum
- Partition the array into two odd length groups with minimized absolute difference between their median
- Minimum window size containing atleast P primes in every window of given range
- Find maximum of minimum for every window size in a given array
- Median
- Geometric Median
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.


