Datasets:

ArXiv:
License:
File size: 500 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import java.io.*;
import java.util.*;

public class Kth{
  public static void main(String[] args) {
    
    int a[] = {4,5,2,1,3,12,11};
    int n = a.length;

    int k = 3;

    PriorityQueue<Integer> q = new PriorityQueue<>();

    for(int i = 0; i <n; i++){

      if(i < k){

        q.add(a[i]);
      }
      else{

        if(a[i] > q.peek()){
          q.remove();
          q.add(a[i]);
        }
      }
    }

    while(!q.isEmpty()){
      System.out.print(q.remove()+" ");
    }
  }
}