Prepin
Log in

Determine the number of rotations in a circularly sorted array.

Approach / Explanation

This question requires you to determine how many times a sorted array has been rotated in a circular fashion. In a circularly sorted array, when the array surpasses its maximum limit, it starts over from the beginning. If all the elements are unique, the number of rotations corresponds to the index of the minimum element. However, if there are repeating elements, simply identifying the minimum is insufficient; you'll need to implement a specific algorithm to accomplish this task. When tackling this question, consider using binary search, as it provides an efficient solution. The important concepts to grasp include the characteristics of a rotated sorted array: At least one half of the array (either the left or the right side) is always sorted. The minimum element serves as the pivot, with the number of rotations being equal to the index of this element. As you develop your solution: Utilize binary search for optimization. Determine if the mid element is the minimum. If it isn’t, proceed to the unsorted side, where the minimum element will be located.

Suggested Answer

Let's consider the array: [11, 12, 15, 18, 2, 5, 6, 8]. First, we need to analyze the array using the concept of binary search. If the middle element is smaller than both of its neighboring elements, then it is the minimum. If the middle element is greater than the element at the high index, the minimum element is located to the right (between high and mid); otherwise, it is found to the left (between low and mid). Now, let's apply these steps to our example: Initially, we have low=0 and high=7 (mid=3). The mid element is 18, which is greater than the element at the high index. Therefore, the minimum element must be on the right side. We update low to mid+1, resulting in low=4. Next, with low=4 and high=7 (mid=5), the mid element is 5, which again is greater than the high index element. Thus, the minimum element is still to the right. We set low to mid+1, making low=6. Now, with low=6 and high=7 (mid=6), the mid element is 6, which remains greater than the high index element. Therefore, the minimum element is in the right section again. We update low to mid+1, giving us low=7. Finally, we have low=7 and high=7 (mid=7). We exit the loop, and the answer is 7. Consequently, the array has been rotated 7 times.

Alternative Answer

Let's consider another example array: [5, 6, 9, 11, 15, 1, 3, 4]. Following the same steps, we observe the following: We start with low=0 and high=7 (mid=3). The mid element is 11, which is greater than the element at the high index. Therefore, the minimum element must be in the right half. We update low to mid+1, resulting in low=4. Now, with low=4 and high=7 (mid=5), the mid element is 1, which is smaller than both of its neighboring elements. This indicates that mid is the minimum. Hence, in this example, the number of rotations is 5.

Question Details

Difficulty & Category

Medium
data structures

Companies

Microsoft

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.