There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values).
Before being passed to your function, nums is 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 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.
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
Constraints:
1 <= nums.length <= 5000-104 <= nums[i] <= 104nums is guaranteed to be rotated at some pivot.-104 <= target <= 104
Follow up: This problem is similar to Search in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why?
This approach uses a modified binary search to handle the rotated sorted array with duplicates. Normally, in a binary search, you would compare the middle element with the target and adjust your search range accordingly. However, because this array is rotated, the sorted order is disrupted, and duplicates can complicate matters.
The key steps are:
The time complexity is generally O(log N), but in the worst case with duplicates, it can degrade to O(N).
This Python implementation of the modified binary search accounts for duplicates by checking for duplicate middle elements. If they exist, the algorithm adjusts the search boundaries while maintaining the sorted distinction in either half of the array.
C++
Java
C
C#
JavaScript
The time complexity is O(log N) in the average case, but O(N) in the worst case due to duplicates. The space complexity is O(1) since no extra storage is needed.
In cases of heavy element duplication, when the above methods become inefficient, a linear search ensures completion of the task. Though not ideal for large-sized arrays, this method provides a simple, guaranteed solution without involving complicated logic.
The complexity for linear search is both time and space O(N).
In this simplistic Python solution, we loop through the entire array, checking each element for equality with the target, making it a reliable brute-force method.
C++
Java
C
C#
JavaScript
Both time and space complexities are O(N), since we traverse the entire array.
| Approach | Complexity |
|---|---|
| Approach 1: Modified Binary Search | The time complexity is O(log N) in the average case, but O(N) in the worst case due to duplicates. The space complexity is O(1) since no extra storage is needed. |
| Approach 2: Linear Search (Fallback) | Both time and space complexities are O(N), since we traverse the entire array. |
Search in rotated sorted array - Leetcode 33 - Python • NeetCode • 413,348 views views
Watch 9 more video solutions →Practice Search in Rotated Sorted Array II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor