Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find Kth Rotation

Easy Acceptance 23.16% Points 20.00

Given an increasing sorted rotated array arr of distinct integers. The array is right-rotated k times. Find the value of k.

Let's suppose we have an array arr = [2, 4, 6, 9], so if we rotate it by 2 times so that it will look like this:

After 1st Rotation : [9, 2, 4, 6]

After 2nd Rotation : [6, 9, 2, 4]

Examples
Example 1

Example 1

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

Output: 1

Explanation: The given array is 5 1 2 3 4. The original sorted array is 1 2 3 4 5. We can see that the array was rotated 1 times to the right.

Example 2

Example 2

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

Output: 0

Explanation: The given array is not rotated.

Hints
Hint 1
Expected Time Complexity: O(log n)
Hint 2
Expected Space Complexity: O(1)
Constraints
  • 1 <= n <= 10^5
  • 1 <= arr[i] <= 10^7
Companies
Amazon Accenture ServiceNow Flipkart
Topics
Array Stack Searching Algorithm
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