Given that integers are being read from a data stream. Find the median of all the elements read so far starting from the first integer until the last integer. This is also called Median of Running Integers.
The given link already contains solution of this problem using Priority Queue.
However, the following solution uses the same concept but the implementation is by using sets.
In this solution, we will print the smaller median in case of even length instead of taking their average.
Examples:
Input: arr[] = {-10, 14, 11, -5, 7}
Output: -10 -10 11 -5 7Input: arr[] = {2, -5, 14}
Output: 2 -5 2
Approach:
- Create two multisets g in ascending order that will store the upper half and s in descending order to store the lower half of the array arr[].
- Insert the first element in s. And initialize the median with this value.
- For every other element of the array x. Check the sizes of both the sets:
- When size(s) > size(g): If x > median then insert the first element of s into g and remove that element from s, insert x into s. Else insert x into g.
- When size(s) < size(g): If x < median then insert the first element of g into s and remove that element from g, insert x into g. Else insert x into s.
- When size(s) = size(g): If x > median. Insert x into s. Else insert x into g.
Below is the implementation of the above approach:
// C++ program to find running median for // a stream of integers using Set #include <bits/stdc++.h> using namespace std; // Function to print the running median // for the array arr[] void printRunningMedian(int arr[], int n) { // Multiset is used to handle duplicates // Multiset g for storing upper half // (ascending order) // The first element will be the smallest) multiset<int> g; // Multiset s for storing lower half // (descending order). The first element // will be the largest multiset<int, greater<int> > s; s.insert(arr[0]); // Initialise median with the first element int med = arr[0]; printf("%d ", med); for (int i = 1; i < n; i++) { // Only add elements to upper half if // its size less then the size of the // lower half (maintain only difference // of 1) if (s.size() > g.size()) { if (arr[i] < med) { int temp = *s.begin(); s.erase(s.begin()); g.insert(temp); s.insert(arr[i]); } else g.insert(arr[i]); med = *s.begin() > *g.begin() ? *g.begin() : *s.begin(); } // Only add elements to lower half if // it's size less then the size of the // upper half (maintain only difference // of 1) else if (s.size() < g.size()) { if (arr[i] > med) { int temp = *g.begin(); g.erase(g.begin()); s.insert(temp); g.insert(arr[i]); } else s.insert(arr[i]); med = *s.begin() > *g.begin() ? *g.begin() : *s.begin(); } // If sizes are same else { if (arr[i] > med) { g.insert(arr[i]); med = *g.begin(); } else { s.insert(arr[i]); med = *s.begin(); } } printf("%d ", med); } printf("\n"); } // Driver code int main() { int arr[] = { -10, 14, 11, -5, 7 }; int n = sizeof(arr) / sizeof(arr[0]); printRunningMedian(arr, n); return 0; } |
-10 -10 11 -5 7
Recommended Posts:
- Background Subtraction in an Image using Concept of Running Average
- Running previous command with sudo
- How to make sure that Apache service keeps running in Ubuntu
- How to Get the Id of a Current Running Thread in Java?
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Median of sliding window in an array | Set 2
- Finding Median of unsorted Array in linear time using C++ STL
- Median of an unsorted array using Quick Select Algorithm
- How To Make Histogram with Median Line using Altair in Python?
- Finding Mean, Median, Mode in Python without libraries
- Geometric Median
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Partition the array into two odd length groups with minimized absolute difference between their median
- Find K elements whose absolute difference with median of array is maximum
- Minimum increment/decrement operations required to make Median as X
- Change in Median of given array after deleting given elements
- Pandas Groupby and Computing Median
- Reverse elements of a Parallel Stream in Java
- 10 Ways to Create a Stream in Java
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
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.


