Search in Rotated Sorted Array

Advanced

In this challenge, you've stumbled upon a magical list of numbers, but it seems like someone has rotated the list in a mystical way! Your job is to find a specific number inside this list.

The list is sorted in ascending order but then gets rotated. This means some numbers from the start of the list have been moved to the back. For example:

  • Before rotation: [1, 2, 3, 4, 5, 6, 7]
  • After rotation: [4, 5, 6, 7, 1, 2, 3]

Your task is to write a function search_in_rotated_array that takes in two arguments:

  1. A rotated_list of unique integers.
  2. A target number you are searching for.

The function should return the index of the target in the rotated_list. If the target is not found, return -1.

Here's how your function should work:

# Example 1: rotated_list = [4, 5, 6, 7, 0, 1, 2] target = 0 search_in_rotated_array(rotated_list, target) # Should return 4 # Example 2: rotated_list = [4, 5, 6, 7, 0, 1, 2] target = 3 search_in_rotated_array(rotated_list, target) # Should return -1 # Example 3: rotated_list = [1] target = 0 search_in_rotated_array(rotated_list, target) # Should return -1

Hint: Think about the properties of a rotated sorted array and how binary search could be applied by checking which half is properly sorted. Good luck with your search!

Geek
Inputs

Arg 1:

[4,5,6,7,0,1,2]

Arg 2:

0
Expected Output
4
Actual Output