/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 sorted in ascending order and possibly rotated at an unknown index, 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 = [15,18,2,3,6,12], search_value = 3

Output: 3

Explanation: 3 is found at index 3.

Example 2

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

Output: 1

Explanation: 10 is found at index 1.

Example 3

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

Output: -1

Explanation: 5 is not present in the array.

Constraints

  • 1 <= len(rotated_array) <= 5000
  • -10000 <= rotated_array[i] <= 10000
  • All elements in rotated_array are unique
  • rotated_array is sorted in ascending order and possibly rotated
  • -10000 <= search_value <= 10000
Python (current runtime)

Case 1

Input: rotated_array = [22,25,30,2,5,10,15], search_value = 5

Expected: 4

Case 2

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

Expected: 3

Case 3

Input: rotated_array = [5], search_value = 5

Expected: 0

Case 4

Input: rotated_array = [5], search_value = 10

Expected: -1

Case 5

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

Expected: 6