# 0033-search-in-rotated-sorted-array Try it on leetcode ## Description

There is an integer array nums sorted in ascending order (with distinct values).

Prior to being passed to your function, nums is possibly left rotated at an unknown index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be left rotated by 3 indices and become [4,5,6,7,0,1,2].

Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.

You must write an algorithm with O(log n) runtime complexity.

 

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

Example 3:

Input: nums = [1], target = 0
Output: -1

 

Constraints:

## Solution(Python) ```Python class Solution: def search(self, nums: List[int], target: int) -> int: # 4 5 6 7 0 1 2 # 7 > 0 # l r 4 2 # mid 7 mid + 1 0 7 > 0 return mid + 1 # l = mid + 1 # 456012 def findpivot(l,r): while l < r: mid = (l + r) // 2 if nums[r] < nums[mid]: l = mid + 1 else: r = mid return r def binarysearch(l ,r): while l <= r: mid = (l + r) // 2 if nums[mid] == target: return mid elif nums[mid] > target: r = mid -1 else: l = mid + 1 return -1 n = len(nums) if n == 1: return 0 if nums[0] == target else -1 k = findpivot(0, n - 1) if k == -1: k = n -1 if nums[k] <= target <= nums[n-1]: return binarysearch(k, n-1) else: return binarysearch(0, k-1) ```