Introduction
The Find Number of Rotations in a Sorted Array problem is a very common DSA interview question and usually comes right after questions about rotated arrays.
In simple words, this problem asks you to find how many times a sorted array has been rotated.
Real-World Meaning of Number of Rotations
Imagine a circular dial with numbers written in increasing order:
0 1 2 3 4 5 6
If the dial is rotated, the order looks different, but the smallest number shows how many steps the dial was rotated.
In the same way, in a rotated sorted array, the position of the minimum element tells us the number of rotations.
What Does “Number of Rotations” Mean?
A sorted array rotated k times means:
The array was shifted
kpositions to the right (or left, depending on definition)The relative order of elements remains the same
Example
Original: [1, 2, 3, 4, 5]
Rotated: [3, 4, 5, 1, 2]
Here, the array is rotated 2 times.
Key Observation (Very Important)
The number of rotations in a sorted array is equal to:
The index of the minimum element
Once we find the minimum element efficiently, the problem is solved.
Problem Statement
You are given a rotated sorted array with unique elements. Find the number of rotations.
Example
Array: [15, 18, 2, 3, 6, 12]
Output: 2
Explanation:
The smallest element is
2It appears at index
2So, the array is rotated 2 times
Before vs After Understanding
Linear Scan (Before)
Check every element
Find the smallest
Return its index
Works, but slow for large arrays.
Binary Search (After)
Use array properties
Reduce search space by half
Find minimum efficiently
Why Binary Search Works Here
In a rotated sorted array:
One half is always sorted
The minimum element lies in the unsorted half
Binary search helps us directly move toward that half.
What Interviewers Are Actually Testing
Interviewers want to see:
Do you understand rotation logic?
Can you reuse binary search ideas?
Can you convert observation into code?
This problem tests pattern recognition, not memorization.
Key Idea (Very Simple)
If middle element is greater than the rightmost element
Minimum is on the right side
Else
Minimum is on the left side or at mid
The index of that minimum element is the answer.
Step-by-Step Logic
Set
low = 0,high = n - 1Find
midCompare
arr[mid]witharr[high]If
arr[mid] > arr[high], movelowtomid + 1Else, move
hightomidContinue until
low == high
Join the conversation! Your thoughts help the community grow.