fork(2) 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. static int getRotationCount(int[] arr){
  11. if (arr == null)
  12. return 0;
  13. int low = 0;
  14. int high = arr.length-1;
  15. while (arr[low] > arr[high])
  16. {
  17. // find mid.
  18. int mid = (low + high)/2;
  19. // decide which sub-array to continue with.
  20. if (arr[mid] > arr[high])
  21. low = mid + 1;
  22. else
  23. high = mid;
  24. }
  25. return low;
  26. }
  27.  
  28. public static void main (String[] args) throws java.lang.Exception
  29. {
  30. // your code goes here
  31. int[] A = { 7, 8, 9, 10, 1, 2, 3, 4, 5, 6};
  32. System.out.println("The array is rotated " + getRotationCount(A) + " times");
  33. }
  34. }
Success #stdin #stdout 0.09s 27796KB
stdin
Standard input is empty
stdout
The array is rotated 4 times