From 90513c69e88880e7a41724d1482342ed65fa5ea6 Mon Sep 17 00:00:00 2001 From: Srijan Date: Thu, 20 Oct 2022 23:10:19 +0530 Subject: [PATCH] algo to print continuos medians --- MAANG-DSA-Prep/medianOfStreamofIntegers.cpp | 56 +++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 MAANG-DSA-Prep/medianOfStreamofIntegers.cpp diff --git a/MAANG-DSA-Prep/medianOfStreamofIntegers.cpp b/MAANG-DSA-Prep/medianOfStreamofIntegers.cpp new file mode 100644 index 0000000..dd76a55 --- /dev/null +++ b/MAANG-DSA-Prep/medianOfStreamofIntegers.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +// TC:O(nLogn) +// SC:O(n) + +void continuosMedian(int arr[], int n) +{ + float median; + // stores the left half of the list but in a max heap to get the greatest value from it + priority_queue maxheap; + // stores the right half of the list but in a min heap to get the smallest value from it + priority_queue, greater> minheap; + for (int i = 0; i < n; i++) + { + // Step 1:input elements in the maxHeap + int current = arr[i]; + if (maxheap.empty() || maxheap.top() > current) + maxheap.push(current); + else + minheap.push(current); + + // Step 2:maintain the difference <=1 + if (minheap.size() > maxheap.size() + 1) + { + maxheap.push(minheap.top()); + minheap.pop(); + } + else + { + minheap.push(maxheap.top()); + maxheap.pop(); + } + + // Step 3:decide for the way to find median based on following conditions + if (n % 2 == 1) // i.e elements are odd + { + if (minheap.size() > maxheap.size()) + median = minheap.top(); + else + median = maxheap.top(); + } + else + { + median = (maxheap.top() + minheap.top()) / 2; + } + cout << median << " "; + } +} +int main() +{ + int arr[] = {5, 15, 1, 3, 2, 8, 7, 9, 10, 6, 11, 4}; + int n = sizeof(arr) / sizeof(arr[0]); + cout << "Current Median at every stage:"; + continuosMedian(arr, n); + return 0; +} \ No newline at end of file