Solution: Search in Rotated Sorted Array
Let's solve the Search in Rotated Sorted Array problem using the Modified Binary Search pattern.
Statement#
Given a sorted integer array, nums, and an integer value, target, the array is rotated by some arbitrary number. Search and return the index of target in this array. If the target does not exist, return -.
An original sorted array before rotation is given below:
After rotating this array 6 times, it changes to:
Constraints:
- All values in
numsare unique. - The values in
numsare sorted in ascending order. - The array may have been rotated by some arbitrary number.
-
nums.length -
nums[i] -
target
Solution#
So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
Naive approach#
A naive approach is to traverse the whole array while searching for our target.
We get the required solution, but at what cost? The time complexity is because we traverse the array only once, and the space complexity is . Let’s see if we can use the modified binary search pattern to design a more efficient solution.
Optimized approach using modified binary search#
We’ve been provided with a rotated array to apply binary search, which is faster than the above linear approach. We can change the part we have to search based on our three pointers—low, mid, and high.
The slides below illustrate how we would like the algorithm to run:
1 of 13
2 of 13
3 of 13
4 of 13
5 of 13
6 of 13
7 of 13
8 of 13
9 of 13
10 of 13
11 of 13
12 of 13
13 of 13
Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.
Step-by-step solution construction#
Let’s start with learning how to use binary search to find a target value in an unrotated sorted array. We can do this either iteratively or recursively. Let’s look at the iterative version first.
Next, let’s look at the recursive version.
Binary search works with arrays that are completely sorted. However, the nums array that we’re provided may not have this property if it’s rotated. Therefore, we have to modify our binary search to accommodate this rotation.
You may notice that at least one half of the array is always sorted—if the array is rotated by less than half the length of the array, at least the second half of the array will still be sorted. Contrarily, if the array is rotated by more than half the length of the array, then at least the first half of the array will be sorted. We can use this property to our advantage and modify the binary_search function as follows:
-
If the
targetvalue lies within the sorted half of the array, our problem is a basic binary search. -
Otherwise, discard the sorted half and keep examining the unsorted half.
Here is how we go about implementing the iterative approach for this:
The recursive approach is shown below:
The recursive approach:
Solution summary#
To recap, the solution to this problem can be divided into the following five parts:
-
Divide the array into two halves.
-
Check if the first half is sorted, and if it is, do the following.
-
Check if the target lies in this range, and if it does, perform a binary search on this half for the target value.
-
If the target does not lie in this range, move to the second half of the array.
-
-
If the first half is not sorted, check if the target lies in the second half.
-
If the target lies in this half, perform a binary search on this half for the target value.
-
If the target does not lie in the second half, examine the first half.
-
-
If the target value is not found at the end of this process, we return -.
Time complexity#
The time complexity of both approaches is since we divide the array into two halves at each step.
Space complexity#
The space complexity of the iterative solution is since no new data structure is being created.
The space complexity of the recursive solution is , where is the number of elements present in the array and is the maximum number of recursive calls needed to find the target.
Search in Rotated Sorted Array
Final Remarks