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. { private static int findPivot(int arr[],int start,int end)
  10. {
  11. if(start<=end)
  12. {
  13. if(start==end)
  14. return start;
  15. else if(start+1==end)
  16. {
  17. if(arr[start]<=arr[end])
  18. return start;
  19. else
  20. return end;
  21. }
  22. int mid=(start+end)/2;
  23. if(mid<end)
  24. {
  25. if(arr[mid]>arr[mid+1])
  26. return mid+1;
  27. }
  28. if(mid>start)
  29. {
  30. if(arr[mid]<arr[mid-1])
  31. return mid;
  32. }
  33.  
  34. if(arr[mid]<arr[end])
  35. {
  36. return findPivot(arr,start,mid-1);
  37. }
  38. else
  39. return findPivot(arr,mid+1,end);
  40.  
  41. }
  42. return -1;
  43. }
  44. public static void main (String[] args) throws java.lang.Exception
  45. {
  46. // your code goes here
  47. int a[]= {30, 40, 50, 10, 20};
  48. System.out.println("pivot : "+findPivot(a,0,a.length-1));
  49. }
  50. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
pivot : 3