Hone logo
Hone
Problems

Search in Rotated Sorted Array II

You are given a sorted array that has been rotated at some unknown pivot point. This means the array was initially sorted in ascending order, but then a portion of it was moved from the beginning to the end. Your task is to efficiently search for a target value within this rotated array, accounting for the possibility of duplicate elements. This problem is useful in scenarios where you need to search within data that has been partially reordered, such as log files or database records.

Problem Description

The problem requires you to implement a function that takes a sorted array (potentially rotated) and a target value as input. The function should return true if the target value exists within the array, and false otherwise. The array may contain duplicate elements, which can complicate the binary search process. You must handle the rotation and potential duplicates to correctly identify the presence of the target.

Key Requirements:

  • The array is initially sorted in ascending order.
  • The array has been rotated at an unknown pivot point.
  • The array may contain duplicate elements.
  • The function must return true if the target is found, false otherwise.

Expected Behavior:

The function should efficiently search the rotated array for the target value. It should handle cases where the target is present at the beginning, middle, or end of the original sorted array, as well as cases where the target is not present. The presence of duplicates requires careful consideration to avoid infinite loops or incorrect results.

Edge Cases to Consider:

  • Empty array: Should return false.
  • Array with a single element: Should return true if the element matches the target, false otherwise.
  • Target value is smaller than the smallest element in the array.
  • Target value is larger than the largest element in the array.
  • Array with all duplicate elements.
  • Target value is equal to multiple elements in the array.

Examples

Example 1:

Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Explanation: 0 exists in the array.

Example 2:

Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Explanation: 3 does not exist in the array.

Example 3:

Input: nums = [1,3,1,1,1], target = 3
Output: true
Explanation: 3 exists in the array.

Example 4:

Input: nums = [1,1,1,1,1,1,1,1,1,13,1,1,1,1,1,1,1,1,1,1,1,1], target = 13
Output: true
Explanation: 13 exists in the array.

Constraints

  • 1 <= nums.length <= 10000
  • -2^31 <= nums[i] <= 2^31 - 1
  • -2^31 <= target <= 2^31 - 1
  • The array nums may contain duplicate elements.
  • The time complexity should be O(log n) on average, but O(n) in the worst case due to duplicates.

Notes

Consider adapting binary search to handle the rotation. The key is to determine which half of the array is sorted and then decide whether to search that half based on the target value. Be mindful of how duplicates can affect the binary search logic and potentially lead to incorrect decisions. When encountering duplicates, you might need to advance the left or right pointer to break the symmetry and continue the search. A simple check to see if nums[left] == nums[mid] is crucial for handling duplicates correctly.

Loading editor...
plaintext