441. Number of Times Array is Rotated

EasyBinary SearchArrayBinary Search

A sorted array of distinct integers was rotated k times (0 ≤ k < n). Given the rotated array, return k — the number of times it was rotated. This equals the index of the minimum element. Solve in O(log n).

Input: A rotated sorted array of distinct integers.

Output: Number of rotations (index of the minimum element).

Examples

Example 1
Input: [4,5,6,7,0,1,2]
Output: 4
Explanation: Minimum 0 is at index 4; array was rotated 4 times.
Example 2
Input: [1,2,3,4,5]
Output: 0
Explanation: Not rotated; minimum is at index 0.
Example 3
Input: [3,4,5,1,2]
Output: 3
Explanation: Minimum 1 is at index 3.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →