ntree.ai
ntree.aiSearch in Rotated Sorted Array

Search in Rotated Sorted Array

mediumsolvedattempted

You are given an array of integers nums with all different elements. It was sorted in ascending order and then rotated, so some number of elements from the start were moved to the end.

Return the index of target in nums, or -1 if the array does not contain it.

Example 1:

Input:  nums = [4, 5, 6, 7, 0, 1, 2], target = 0
Output: 4

Example 2:

Input:  nums = [4, 5, 6, 7, 0, 1, 2], target = 3
Output: -1

Constraints:

  • 1 <= length of nums <= 5000
  • -10^4 <= nums[i] <= 10^4
  • -10^4 <= target <= 10^4
def search(nums: list[int], target: int) -> int:
pass
 

Press Visualise to step through your code.