Design Gurus Logo
Blind 75
Problem Challenge 2: Search in Rotated Array (medium)

Problem Statement

Given an array of numbers which is sorted in ascending order and also rotated by some arbitrary number, find if a given ‘key’ is present in it.

Write a function to return the index of the ‘key’ in the rotated array. If the ‘key’ is not present, return -1. You can assume that the given array does not have any duplicates.

Note: You need to solve the problem in O(logn) time complexity.

Example 1:

Input: [10, 15, 1, 3, 8], key = 15
Output: 1
Explanation: '15' is present in the array at index '1'.
Image

Example 2:

Input: [4, 5, 7, 9, 10, -1, 2], key = 10
Output: 4
Explanation: '10' is present in the array at index '4'.
Image

Constraints:

  • 1 <= arr.length <= 5000
  • -10<sup>4</sup> <= arr[i] <= 10<sup>4</sup>
  • All values of nums are unique.
  • arr is an ascending array that is possibly rotated.
  • -10<sup>4</sup> <= key <= 10<sup>4</sup>

Try it yourself

Try solving this question here:

Python3
Python3