fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int arr[]={-1,0,3,5,9,12};
  13. int k=9;
  14. int res = kthSmallest(arr,k);
  15. System.out.println(res);
  16. }
  17.  
  18. public static int kthSmallest(int[] nums,int k) {
  19.  
  20. int n=nums.length;
  21. int low=0,high=n-1;
  22. k = k % n;
  23. while(low <= high){
  24. int mid = low +(high - low) / 2;
  25. if(mid == k-1){
  26. return nums[mid];
  27. }else if(mid < k-1){
  28. low = mid + 1;
  29. }else if(mid > k-1){
  30. high = mid -1 ;
  31. }
  32. }
  33. return Integer.MIN_VALUE;
  34. }
  35. }
Success #stdin #stdout 0.07s 54524KB
stdin
Standard input is empty
stdout
3