There is an integer array nums sorted in ascending order (with possible duplicates).
Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (0 <= 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,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4].
Given the array nums after the possible rotation and an integer target, return true if target is in nums, or false if it is not in nums.
You must decrease the overall operation steps as much as possible.
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
This is similar to Search in Rotated Sorted Array I, but duplicates make it harder to determine which half is sorted. When nums[left] == nums[mid] == nums[right], we cannot determine which side is sorted, so we must skip the duplicate by incrementing left.
This is Modified Binary Search on Rotated Array with Duplicates with:
Initialize: left = 0, right = n - 1
Binary Search Loop: While left <= right:
mid = left + (right - left) / 2nums[mid] == target: Return trueHandle Duplicates:
nums[left] == nums[mid] == nums[right]: Cannot determine sorted half, increment leftIdentify Sorted Half:
nums[left] <= nums[mid]: Left half is sorted
nums[left] <= target < nums[mid]: Search leftnums[mid] < target <= nums[right]: Search rightNot Found: Return false