fork download
  1. import java.util.Scanner;
  2. class Ideone
  3. {
  4. public static void main (String[] args) throws java.lang.Exception
  5. {
  6. Scanner scr = new Scanner(System.in);
  7. int n = scr.nextInt(); // no. of elements in array
  8. int[] a = new int[n];
  9. for(int i=0;i<n;i++)
  10. // array elements
  11. a[i] = scr.nextInt();
  12.  
  13. int k = scr.nextInt(); // k th smallest element
  14. int[] b = new int[k];
  15.  
  16. // creating new array b with first k elements
  17. for(int i=0;i<k;i++)
  18. b[i] = a[i];
  19.  
  20. int ans = max(b); // max element's index
  21.  
  22. //the real deal
  23. for(int i=k;i<n;i++){
  24. if(a[i] < b[ans])
  25. b[ans] = a[i];
  26. ans = max(b);
  27. }
  28. System.out.println( b[ans]);
  29.  
  30. }
  31. public static int max(int[] a){
  32. int m =0,n = a.length;
  33. for(int i=1;i<n;i++){
  34. if(a[m] < a[i])
  35. m = i;
  36. }
  37. return m;
  38.  
  39. }
  40. }
Success #stdin #stdout 0.06s 4386816KB
stdin
5
12
23
1
34
56
3
stdout
23