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. Scanner s=new Scanner(System.in);
  13. int a[]={-100,-23,-5,0,12,19,27,36,79,100,191};
  14. int low=0,high=a.length-1;
  15. int key=90,ans=-1;
  16. ans=binarySearch(a,low,high,key);
  17. System.out.println(ans);
  18.  
  19. }
  20. public static int binarySearch(int A[],int low,int high,int key){
  21.  
  22. if(low==high){
  23. if(A[low]==key){
  24. return low;
  25. }
  26. else{
  27. return 0;
  28. }
  29. }
  30. else{
  31. if(low>=high){
  32. return -1;
  33. }
  34. int mid=low+(high-low)/2;
  35. if(A[mid]==key){
  36. return mid;
  37. }
  38. if(key<A[mid]){
  39. return binarySearch(A,low,mid-1,key);
  40. }
  41. else{
  42. return binarySearch(A,mid+1,high,key);
  43. }
  44. }
  45.  
  46.  
  47. }
  48. }
Success #stdin #stdout 0.11s 56616KB
stdin
Standard input is empty
stdout
-1