Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Search In Rotated Sorted Array

Medium Acceptance 37.64% Points 30

Given a sorted (in ascending order) and rotated array arr of distinct elements which may be rotated at some point and given an element key, the task is to find the index of the given element key in the array arr. The whole array arr is given as the range to search.

Rotation shifts elements of the array by a certain number of positions, with elements that fall off one end reappearing at the other end.

Note: 0-based indexing is followed & returns -1 if the key is not present.

Examples
Example 1

Example 1

Input: arr[] = [5, 6, 7, 8, 9, 10, 1, 2, 3], key = 10

Output: 5

Explanation: 10 is found at index 5.

Example 2

Example 2

Input: arr[] = [3, 5, 1, 2], key = 6

Output: -1

Explanation: There is no element that has value 6.

Constraints
  • 1 <= arr.size() <= 10^6
  • 0 <= arr[i] <= 10^6
  • 1 <= key <= 10^5
Hints
  • Expected Time Complexity: O(log n)
  • Expected Space Complexity: O(1)
Topics
Array Searching Algorithms
Companies
Amazon Microsoft Adobe Google Intuit DE Shaw Flipkart Paytm
Added 19 Jul 2026 37.64% acceptance
Ln 1, Col 1Sign in to save your code
4 5 6 7 0 1 2
0
4