/33. Search in Rotated Sorted Array

33. Search in Rotated Sorted Array

Medium
Arrays44.2% acceptance

Given a list of unique integers rotated_array, which is a sorted array in ascending order that has been rotated at an unknown pivot, and an integer search_value, return the index of search_value in rotated_array. If search_value is not present, return -1. The solution must run in O(log n) time.

Example 1

Input: rotated_array = [10,12,15,1,3,5,7], search_value = 3

Output: 4

Explanation: 3 is at index 4 in the rotated array.

Example 2

Input: rotated_array = [22,25,30,2,5,8,12], search_value = 25

Output: 1

Explanation: 25 is at index 1 in the rotated array.

Example 3

Input: rotated_array = [5], search_value = 5

Output: 0

Explanation: 5 is at index 0.

Example 4

Input: rotated_array = [9,11,13,15,1,3,5,7], search_value = 8

Output: -1

Explanation: 8 is not present in the array.

Constraints

  • 1 <= len(rotated_array) <= 5000
  • -104 <= rotated_array[i] <= 104
  • All elements in rotated_array are unique
  • rotated_array is a sorted array in ascending order, possibly rotated at an unknown index
  • -104 <= search_value <= 104
Python (current runtime)

Case 1

Input: rotated_array = [17,19,21,25,1,3,5,7,11,13], search_value = 11

Expected: 8

Case 2

Input: rotated_array = [50,60,70,80,10,20,30,40], search_value = 70

Expected: 2

Case 3

Input: rotated_array = [100,200,300,400,10,20,30,40,50], search_value = 500

Expected: -1

Case 4

Input: rotated_array = [2,3,4,5,6,7,8,9,1], search_value = 1

Expected: 8

Case 5

Input: rotated_array = [1], search_value = 2

Expected: -1