-
Notifications
You must be signed in to change notification settings - Fork 17
/
MedianOfStream.java
43 lines (40 loc) · 1.46 KB
/
MedianOfStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package SummerTrainingGFG.Heap;
import java.util.Collections;
import java.util.PriorityQueue;
/**
* @author Vishal Singh
* 17-01-2021
*/
public class MedianOfStream {
public static void medianOfStream(int[] arr, int n){
PriorityQueue<Integer> greaterHalf = new PriorityQueue<>();
PriorityQueue<Integer> smallerHalf = new PriorityQueue<>(Collections.reverseOrder());
smallerHalf.add(arr[0]);
System.out.println(arr[0]);
for (int i = 1; i < arr.length; i++) {
int temp = arr[i];
if (smallerHalf.size() > greaterHalf.size()){
if (smallerHalf.peek() > temp){
greaterHalf.add(smallerHalf.poll());
smallerHalf.add(temp);
}else {
greaterHalf.add(temp);
}
if (smallerHalf.peek()!=null && greaterHalf.peek()!=null)
System.out.println((double) (smallerHalf.peek() + greaterHalf.peek()) / 2);
}else {
if (smallerHalf.peek()!=null && temp <= smallerHalf.peek()){
smallerHalf.add(temp);
}else {
greaterHalf.add(temp);
smallerHalf.add(greaterHalf.poll());
}
System.out.println(smallerHalf.peek());
}
}
}
public static void main(String[] args) {
int[] arr = {25, 7, 10, 15, 20};
medianOfStream(arr,arr.length);
}
}