-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind_median_in_stream.java
86 lines (73 loc) · 1.86 KB
/
find_median_in_stream.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
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
/*Given an input stream of n integers the task is to insert integers to stream and print the median of the new stream formed by each insertion of x to the stream.
Example
Flow in stream : 5, 15, 1, 3
5 goes to stream --> median 5 (5)
15 goes to stream --> median 10 (5, 15)
1 goes to stream --> median 5 (5, 15, 1)
3 goes to stream --> median 4 (5, 15, 1, 3)
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class gfg {
public static void main (String[] args) {
//code
Scanner sc= new Scanner(System.in);
int n= sc.nextInt();
int arr[]= new int[n];
int i=0;
while(i<n)
{
arr[i]= sc.nextInt();
Arrays.sort(arr );
for (int p = 0; p < arr.length / 2; p++) {
int temp = arr[p];
arr[p] = arr[arr.length - 1 - p];
arr[arr.length - 1 - p] = temp;
}
if((i+1)%2!=0) // 5,15,1,3 5,10,5,4
System.out.println(arr[i/2]);
else
System.out.println((arr[i/2]+arr[i/2+1])/2);
i++;
}
}
}
// EFFICIENT CODE FOR THE SAME
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
/*public static void sort(int a[],int n){
int temp;
for(int j=0;j<n-1;j++){
for(int k=j;k<n;k++){
if(a[k]<a[j]){
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
}
}*/
public static void main (String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int i;
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=in.nextInt();
for(i=0;i<n;i++){
if(i==0)
System.out.println(a[i]);
else{
Arrays.sort(a,0,i+1);
if(i%2==0)
System.out.println(a[i/2]);
else
System.out.println((a[i/2]+a[i/2+1])/2);
}
}
//code
}
}