-
Notifications
You must be signed in to change notification settings - Fork 33
/
kthLargestELement.java
43 lines (35 loc) · 1.08 KB
/
kthLargestELement.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 whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tests = Integer.parseInt(br.readLine());
for(int t = 1 ; t <= tests ; t++)
{
String ar[] = br.readLine().split(" ");
int k = Integer.parseInt(ar[0]);
int n = Integer.parseInt(ar[1]);
ar = br.readLine().split(" ");
PriorityQueue<Integer> pq = new PriorityQueue<>(); //min heap
for(int i = 0 ; i < k-1 ; i++)
{
pq.add(Integer.parseInt(ar[i]));
System.out.print("-1 ");
}
pq.add(Integer.parseInt(ar[k-1]));
System.out.print(pq.peek() + " ");
for(int i = k ; i < n ; i++)
{
if(pq.peek() < Integer.parseInt(ar[i]))
{
pq.poll();
pq.add(Integer.parseInt(ar[i]));
}
System.out.print(pq.peek() + " ");
}
System.out.println(" ");
}
}
}