educative.io

Educative

Is the following solution also acceptable?

public static int countRotations(int[] arr) {
   
    if(arr == null || arr.length == 0) {
      return -1;
    }
    else if(arr[0] < arr[arr.length - 1]) {
      return 0;
    }

    int left = 0;
    int right = arr.length - 1;
    int mid = right / 2;
    while(left < right) {
      if(arr[left] < arr[mid]) {
        left = mid;
      }
      else {
        right = mid;
      }

      mid = left + (right - left) / 2;
    }

    return mid + 1; 
  }

Type your question above this line.

Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/6063579138621440

@Peter_Litvak Yes the above solution is also acceptable since it’s giving the correct number of rotations for all three of our arrays.