Searching and Sorting in Rotated Sorted Array  Part2
Introduction
In the previous blog on the rotated sorted array, we learned about searching in the rotated sorted array with no duplicate elements. In this blog, we will learn about searching an element in a rotated sorted array with duplicate elements. But before diving into the solution, let's see an example and understand the problem thoroughly
The array given above is sorted from the 8 at the 0th index to 12 and 7 to 8 at the last index. Or we can say that it was a sorted array from 7 to 12, then rotated by three positions in the clockwise direction. Now we have the task of searching the given number in this array. Let's see how we can do that
Solution using modified binary search
The idea to solve this problem is very similar to what we used for searching an element in a rotated sorted array with no duplicate elements. We used a modified binary search for this.
We start with finding the mid element of the array, divide the array into two subarrays around that middle element, and then pick one subarray to follow the above process recursively. Now we know that for a rotated sorted array, one of the subarrays is sorted. So, we compare the left, right, and midvalue to check which subarray is sorted.
At this step in the rotated sorted array with duplicate elements, all three values left, middle, and right elements could be the same. Then, we won't be able to decide which subarray is sorted. For Example:Let nums = [4 5 6 4 4 4 4] ,mid = 3 and lo = 0, since nums[3] >= nums[0] but the array from 0 to 3 is out of order and not sorted, so we need to modify the previous solution to handle this case.
To accommodate the case of left, right, middle elements being equal, we can put a check for this before checking which subarray is sorted. And if all these elements are equal, we will move the left and right counters inwards by one position at a time to find a subarray with different values at the left and right index. let's see an example to develop a better understanding
In the above example, the left, middle and right elements are equal, so we can't decide which subarray is sorted. But, if we move both the left and right counters one position inwards(i.e left = left +1 and right = right  1), the value of right changes to 7, and we can find that the left subarray is sorted.
Algorithm
 Take the array and key from user input.
 Find the middle element of the array as mid= (left+right)/2.
 If the middle element is equal to the key, return mid.
 If left, right, and middle elements are equal increase the left counter by 1 and decrease the right counter by 1, and recursively call step 2, with new left and right values.
 Check if the left subarray is sorted( one of both subarrays is sorted)
 Check the extreme values of the left subarray. If the key lies between it, recursively call step 2 for it.
 Otherwise, recursively call step 2 for the right subarray.
 Otherwise, the right subarray is sorted
 Check the extreme values of the right subarray. If the key lies between it, recursively call step 2 for it.
 Otherwise, recursively call step 2 for the left subarray.
 Keep making recursive calls until we either find the key or reach the base case.
Implementation of the solution

Input
Enter the number of elements in the array and the key. 7 12 Enter array elements 8 8 8 8 12 7 8 
Output
4 
Time Complexity:
The time complexity of this algorithm is O(logN) in average case, as we are using binary search, however in the worst case the time complexity becomes O(N) when all the elements in the array are the same.
Space Complexity:
The space complexity of this algorithm is O(1), as no extra space is required.
Frequently asked questions
 How do you rotate a sorted array?
We can rotate a sorted array by shifting all the elements in the cyclic order, i.e., the first element is moved to the rightmost position while shifting towards the left.
 How do you search for a target value in a rotated sorted array with duplicate elements?
We use a modified version of binary search to search a target value in a rotated sorted array with duplicate elements. It starts with finding the middle element of the array and the array into two subarrays around the middle element. After that, we need to pick one subarray to repeat the process recursively, but before doing that, we check if the left, right, and middle elements are equal. If yes, we increase the left value by one and decrease the right value by one. Then we recursively call the modified binary search with updated values of left and right.
 How to check if an array is sorted?
We can check if an array is sorted or not by traversing through it, and if we don't encounter a number that is smaller than its previous number, it is sorted.
 Which is the fastest algorithm for searching?
Binary search is generally considered the fastest searching algorithm, with time complexity of O(logN) considering the behaviour of the problem to be solved is monotonically increasing or decreasing in nature.
 Which searching algorithm is best for sorted arrays?
The binary search algorithm is best for sorted arrays.
Key takeaways
In this blog, we discussed how we could search an element in a rotated sorted array with duplicate elements
For this task, we use a modified binary search. It also takes the array, leftright indices, and the key as parameters similar to binary search. Then we find the middle element and divide the array into two subarrays. Now instead of simply checking the subarrays, if they can contain the key. We first check if the left, right, and middle elements are equal. If yes, we increase the left index by 1 and decrease the right index by one. Then make a recursive call for this modified binary search function with these updated parameters. Otherwise, we first check which subarray is sorted and then check which subarray can contain the key. We continue this process with recursion till either we reach the base case or find the key.
Visit here to learn more about arrays. And practice similar problems on CodeStudio. If you liked this blog, share it with your friends.
Comments
No comments yet
Be the first to share what you think