Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find Rotation Count

Easy Acceptance 75.00% Points 20.00

Given a rotated sorted array of distinct integers, return the number of times the array has been rotated. The rotation count is equal to the index of the minimum element in the array. The array was originally sorted in ascending order and then rotated clockwise.

Examples
Example 1

Example 1

Input: nums = [15,18,2,3,6,12]

Output: 2

Explanation: The minimum element is 2, which is at index 2. Therefore, the array has been rotated 2 times.

Example 2

Example 2

Input: nums = [7,9,11,12,5]

Output: 4

Explanation: The minimum element is 5 at index 4, so the array has been rotated 4 times.

Example 3

Example 3

Input: nums = [1,2,3,4,5]

Output: 0

Explanation: The array is not rotated. The minimum element is at index 0, so rotation count is 0.

Hints
Hint 1
N/A
Constraints
  • 1 = nums.length = 105
  • -104 = nums[i] = 104
  • All values in nums are distinct
Companies
Apple
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions