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. // your code goes here
  13. int arr[]={3,5,8,8,10,12};
  14. int k=6;
  15. int l=0;
  16. int r=arr.length-1;
  17. int ans=-1;
  18. while(l<=r){
  19. int mid=(l+r)/2;
  20. if(arr[mid]>k){
  21. ans=mid;
  22. r=mid-1;
  23.  
  24. }
  25. else if(arr[mid]<k){
  26. l=mid+1;
  27. }
  28. }
  29. System.out.println(ans);
  30. }
  31. }
Success #stdin #stdout 0.06s 16508KB
stdin
Standard input is empty
stdout
2