-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkth_largest_element_in_a_stream(Heap_application).java
88 lines (75 loc) · 2.09 KB
/
kth_largest_element_in_a_stream(Heap_application).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
87
88
/*
https://practice.geeksforgeeks.org/problems/kth-largest-element-in-a-stream/0
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
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class kth_largest_element_in_a_stream_of_integers {
public static void main (String[] args) {
//code
Scanner sc= new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
PriorityQueue<Integer> pq= new PriorityQueue<Integer>();
int min=Integer.MIN_VALUE;
int k=sc.nextInt();
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
int temp = sc.nextInt();
if(i<k)
{ pq.add(temp);
System.out.print("-1 ");
}
else
{
if(i==k)
{
pq.add(temp);
min=pq.peek();
System.out.print(min+" ");
}
else
{
if(temp>pq.peek())
{
pq.poll();
pq.add(temp);
System.out.print(pq.peek()+" ");
min= pq.peek();
}
else
{
System.out.print(min+" ");
}
}
}
}
}
}
}