πDay 4. Find median in a stream π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given a data stream arr[]
, where integers are read sequentially, determine the median of the elements encountered so far after each new integer is read.
Rules for Median Calculation:
If the number of elements is odd, the median is the middle element.
If the number of elements is even, the median is the average of the two middle elements.
π Example Walkthrough:
Example 1:
Input:
arr = [5, 15, 1, 3, 2, 8]
Output:
[5.0, 10.0, 5.0, 4.0, 3.0, 4.0]
Explanation:
Read
5
β median =5.0
Read
15
β median =(5 + 15) / 2 = 10.0
Read
1
β median =5.0
Read
3
β median =(3 + 5) / 2 = 4.0
Read
2
β median =3.0
Read
8
β median =(3 + 5) / 2 = 4.0
Constraints:
$(1 \leq \text{Number of elements} \leq 10^5)$
$(1 \leq arr[i] \leq 10^6)$
π― My Approach:
Min-Heap & Max-Heap
Key Idea:
Use two heaps to maintain the lower half and upper half of elements efficiently.
Max-Heap (Left Half) β Stores the smaller half of the elements.
Min-Heap (Right Half) β Stores the larger half of the elements.
The median is either:
The max of the left half (if odd elements)
The average of max(left half) and min(right half) (if even elements)
Algorithm Steps:
Insert each number into either max-heap or min-heap:
If
maxHeap
is empty ORnum <= maxHeap.top()
, push intomaxHeap
.Else, push into
minHeap
.
Balance the heaps:
If
maxHeap
is larger thanminHeap
by more than 1, move top ofmaxHeap
tominHeap
.If
minHeap
is larger, move top ofminHeap
tomaxHeap
.
Calculate the median:
If
maxHeap
has more elements, returnmaxHeap.top()
.Else, return
(maxHeap.top() + minHeap.top()) / 2.0
.
π Time and Auxiliary Space Complexity
Time Complexity: (O(N \log N)) (heap insertions & balancing)
Auxiliary Space Complexity: (O(N)) (for storing elements in heaps)
π Solution Code
Code (C++)
class Solution {
public:
vector<double> getMedian(vector<int>& arr) {
priority_queue<int> maxHeap;
priority_queue<int, vector<int>, greater<int>> minHeap;
vector<double> res;
for (int num : arr) {
if (maxHeap.empty() || num <= maxHeap.top()) maxHeap.push(num);
else minHeap.push(num);
if (maxHeap.size() > minHeap.size() + 1) minHeap.push(maxHeap.top()), maxHeap.pop();
else if (minHeap.size() > maxHeap.size()) maxHeap.push(minHeap.top()), minHeap.pop();
res.push_back(maxHeap.size() > minHeap.size() ? maxHeap.top() : (maxHeap.top() + minHeap.top()) / 2.0);
}
return res;
}
};
Code (Java)
class Solution {
public ArrayList<Double> getMedian(int[] arr) {
PriorityQueue<Integer> maxH = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> minH = new PriorityQueue<>();
ArrayList<Double> res = new ArrayList<>();
for (int n : arr) {
maxH.add(n);
minH.add(maxH.poll());
if (maxH.size() < minH.size()) maxH.add(minH.poll());
res.add(maxH.size() > minH.size() ? (double) maxH.peek() : (maxH.peek() + minH.peek()) / 2.0);
}
return res;
}
}
Code (Python)
class Solution:
def getMedian(self, arr):
maxHeap, minHeap = [], []
res = []
for num in arr:
heapq.heappush(maxHeap, -num)
heapq.heappush(minHeap, -heapq.heappop(maxHeap))
if len(maxHeap) < len(minHeap):
heapq.heappush(maxHeap, -heapq.heappop(minHeap))
res.append(float(-maxHeap[0]) if len(maxHeap) > len(minHeap) else (-maxHeap[0] + minHeap[0]) / 2.0)
return res
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated