Skip to main content

Rotations in Circularly Sorted Array

MediumPremium

Find the number of rotations in a circularly sorted array. A circularly sorted array is an array that has been sorted in ascending order but then rotated, meaning that the elements at the end of the array have been moved to the beginning.

Write a function that determines the number of rotations the original sorted array has undergone.

Constraints

  • The array contains distinct integers.
  • The array may contain any number of elements, including an empty array.

Examples

arr = [4, 5, 1, 2, 3] output: 2 Explanation: the sorted array [1, 2, 3, 4, 5] has been rotated 2 times to become [4, 5, 1, 2, 3] arr = [1, 2, 3, 4, 5] output: 0 Explanation: the array is already sorted and has not been rotated, so the output is 0

Can you solve it in O(log n) time complexity?

Can you solve it in O(1) space complexity?