# 81. Search in Rotated Sorted Array II

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

You are given a target value to search. If found in the array return true, otherwise return false.

Example 1:

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

Example 2:

Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false

Follow up:

  • This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
  • Would this affect the run-time complexity? How and why?

# Solution

Approach 1: binary search -- same with 0033 Search in Rotated Sorted Array, but worse case can be linear, e.g. [0,0,0,0,0].

# Code (Python)

Approach 1:

class Solution:
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if not nums:
            return False
        left, right = 0, len(nums) - 1
        while left < right:
            mid = (left + right) // 2
            if target in (nums[left], nums[mid], nums[right]):
                return True
            if nums[mid] > nums[right]:
                # example: [4,5,6,7,0,1,2]
                if nums[left] < target < nums[mid]:
                    right = mid - 1
                else:
                    left = mid + 1
            elif nums[mid] < nums[right]:
                # example: [5,0,1,2,3]
                if nums[mid] < target < nums[right]:
                    left = mid + 1
                else:
                    right = mid - 1
            else: # when nums[mid] == nums[right], move either pointer
                right -= -1
        return True if nums[left] == target else False

# Code (C++)

Approach 1:

class Solution {
public:
    bool search(vector<int>& nums, int target) {
        int head = 0;
        int tail = nums.size() - 1;
        while (head <= tail)
        {
            int mid = head + (tail - head) / 2;
            if (nums[mid] == target)
                return true;
            if (nums[head] < nums[tail])
            {
                if (nums[mid] < target)
                    head = mid + 1;
                else
                    tail = mid - 1;
            }
            else if (nums[head] == nums[tail])
                head++;
            else
            {
                if (nums[mid] < target)
                {
                    if (nums[mid] >= nums[head] || target <= nums[tail])
                        head = mid + 1;
                    else if (target > nums[tail])
                        tail = mid - 1;
                }
                else if (nums[mid] > target)
                {
                    if (nums[mid] <= nums[tail] || target >= nums[head])
                        tail = mid - 1;
                    else if (target < nums[head])
                        head = mid + 1;
                }
            }
        }
        return false;
    }
};