-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkth_largest_element_in_the_stream(using priority queue).java
84 lines (64 loc) · 1.93 KB
/
kth_largest_element_in_the_stream(using priority queue).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
/*
Given an input stream of n integers the task is to insert integers to stream and print the kth largest element in the stream at each insertion.
Example:
Input:
stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...}
k = 3
Output: {-1, -1, 10, 11, 20, 40, 50, 50, ...}
Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two lines. The first line of each test case contains two space separated integers k and n . Then in the next line are n space separated values of the array.
Output:
For each test case in a new line print the space separated values denoting the kth largest element at each insertion, if the kth largest element at a particular insertion in the stream doesn't exist print -1.
Constraints:
1<=T<=100
1<=n,k<=1000
1<=stream[]<=100000
Example:
Input:
2
4 6
1 2 3 4 5 6
1 2
3 4
Output:
-1 -1 -1 1 2 3
3 4
*/
/**
*
* @author shivam
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class kth_largest_element_in_the_stream {
public static void main (String[] args)
{
Scanner s = new Scanner(System.in);
int t = s.nextInt();
int n;
while(t-- > 0){
int k = s.nextInt();
n = s.nextInt();
PriorityQueue<Integer> queue = new PriorityQueue<>();
for(int i=0; i<n; i++){
int buf = s.nextInt();
if(i+1 < k){
System.out.print("-1 ");
queue.add(buf);
} else if(i+1 == k){
queue.offer(buf);
System.out.print(queue.peek() + " ");
}else {
if(buf > queue.peek()){
queue.poll();
queue.offer(buf);
}
System.out.print(queue.peek() + " ");
}
}
System.out.println();
}
return;
}
}