forked from codehouseindia/Everything
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK_largest_elements.java
More file actions
36 lines (27 loc) · 828 Bytes
/
K_largest_elements.java
File metadata and controls
36 lines (27 loc) · 828 Bytes
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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
int k = Integer.parseInt(br.readLine());
// write your code here
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i = 0; i < k;i++){
pq.add(arr[i]);
}
for(int i = k; i< arr.length; i++){
if(arr[i] > pq.peek()){
pq.remove();
pq.add(arr[i]);
}
}
while(pq.size() > 0){
System.out.println(pq.remove());
}
}
}