fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.PriorityQueue;
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static int getLargestKnumber (int[] inputs, int k) {
  11. PriorityQueue<Integer> pq = new PriorityQueue<>(
  12. (a, b) -> {
  13. if (a > b) {
  14. return -1;
  15. } else if (a < b) {
  16. return 1;
  17. }
  18. return 0;
  19. });
  20. Set<Integer> numbers = new HashSet<>();
  21. for (int i = 0; i < inputs.length; i++) {
  22. if (!numbers.contains(inputs[i])) {
  23. pq.add(inputs[i]);
  24. numbers.add(inputs[i]);
  25. }
  26. }
  27.  
  28. for (int i = 0; i < k - 1; i++) {
  29. pq.poll();
  30. }
  31.  
  32. return pq.peek();
  33. }
  34.  
  35. public static void main (String[] args) throws java.lang.Exception
  36. {
  37. int[] inputs = new int[] {1, 1, 2, 6, -1, -2, 3, 5, 5, -10, 20, 30, 8, 11};
  38. System.out.print(getLargestKnumber(inputs, 6));
  39. }
  40. }
Success #stdin #stdout 0.08s 55488KB
stdin
Standard input is empty
stdout
5