forked from GDSC-MITS/Contribute-To-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunning Median
117 lines (110 loc) · 3.72 KB
/
Running Median
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*You are given a stream of 'N' integers. For every 'i-th' integer added to the running list of integers, print the resulting median.
Print only the integer part of the median.
Input Format :
The first line of input contains an integer 'N', denoting the number of integers in the stream.
The second line of input contains 'N' integers separated by a single space.
Output Format :
Print the running median for every integer added to the running list in one line (space-separated).
Input Constraints
0 <= N <= 10 ^ 5
1 <= ARR[i] <= 10 ^ 5
Time Limit: 1 sec
Sample Input 1 :
6
6 2 1 3 7 5
Sample Output 1 :
6 4 2 2 3 4
Explanation of Sample Output 1 :
S = {6}, median = 6
S = {6, 2} -> {2, 6}, median = 4
S = {6, 2, 1} -> {1, 2, 6}, median = 2
S = {6, 2, 1, 3} -> {1, 2, 3, 6}, median = 2
S = {6, 2, 1, 3, 7} -> {1, 2, 3, 6, 7}, median = 3
S = {6, 2, 1, 3, 7, 5} -> {1, 2, 3, 5, 6, 7}, median = 4
Sample Input 2 :
5
5 4 3 2 1
Sample Output 2 :
5 4 4 3 3*/
//Solution to this
import java.util.ArrayList;
import java.util.Collections;
import java.util.PriorityQueue;
public class Running_median {
public static void findMedian(int arr[])
{
PriorityQueue<Integer>minHeap =new PriorityQueue<Integer>();
PriorityQueue<Integer>maxHeap =new PriorityQueue<Integer>(Collections.reverseOrder());
ArrayList<Integer> output = new ArrayList<Integer>();
for(int i =0;i<arr.length;i++)
{
if(minHeap.isEmpty() && maxHeap.isEmpty())
{
maxHeap.add(arr[i]);
//output.add(arr[i]);
}
else if(arr[i]<=maxHeap.peek())
{
maxHeap.add(arr[i]);
if((maxHeap.size()-minHeap.size())>1)
{
minHeap.add(maxHeap.poll());
int median = (maxHeap.peek()+minHeap.peek())/2;
//output.add(median);
arr[i] = median;
}
else if(maxHeap.size() == minHeap.size())
{
int median = (maxHeap.peek()+minHeap.peek())/2;
// output.add(median);
arr[i] = median;
}
else if((maxHeap.size()-minHeap.size())== 1)
{
int median = maxHeap.peek();
//output.add(median);
arr[i] = median;
}
}
else if(arr[i]>maxHeap.peek())
{
minHeap.add(arr[i]);
if((minHeap.size()-maxHeap.size())>1)
{
maxHeap.add(minHeap.poll());
int median = (maxHeap.peek()+minHeap.peek())/2;
//output.add(median);
arr[i] = median;
}
else if(maxHeap.size() == minHeap.size())
{
int median = (maxHeap.peek()+minHeap.peek())/2;
//output.add(median);
arr[i] = median;
}
else if((minHeap.size()-maxHeap.size())== 1)
{
int median = minHeap.peek();
//output.add(median);
arr[i] = median;
}
}
}
// for(int i =0;i<output.size();i++)
// {
// System.out.print(output.get(i)+" ");
// }
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {6,2,1,3,7,5};
findMedian(arr);
for(int i =0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Please trace this while understanding
Hope you like it
And understood how we can do using Heap(inbuild PriorityQueue)