Dsa
Binary Search
Easy
Count Rotations in Sorted Array
Given a sorted and rotated array, write a function to count the number of rotations. A rotation is where the array was rotated at a pivot, thus some elements are moved to the end of the array. The function should run in O(log n) time using binary search.
Input:
- A single line containing space-separated integers representing the rotated sorted array.
Output:
- An integer representing the number of rotations.
Example:
Input:
15 18 2 3 6 12
Output:
2
Key concepts
binary_searcharrayrotation
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now