What modification is needed to perform binary search on a rotated sorted array?
Practice Questions
Q1
What modification is needed to perform binary search on a rotated sorted array?
No modification needed
Use linear search
Modify the mid-point calculation
Use a different algorithm
Questions & Step-by-Step Solutions
What modification is needed to perform binary search on a rotated sorted array?
Step 1: Understand that a rotated sorted array is an array that was originally sorted but then rotated at some pivot point.
Step 2: Identify the left and right pointers to define the search range in the array.
Step 3: Calculate the mid-point using the formula: mid = left + (right - left) / 2.
Step 4: Check if the mid-point value is equal to the target value. If it is, return the mid-point index.
Step 5: Determine which side of the array is sorted by comparing the values at the left, mid, and right pointers.
Step 6: If the left side is sorted, check if the target value lies within this range. If it does, adjust the right pointer to mid - 1. If not, adjust the left pointer to mid + 1.
Step 7: If the right side is sorted, check if the target value lies within this range. If it does, adjust the left pointer to mid + 1. If not, adjust the right pointer to mid - 1.
Step 8: Repeat steps 3 to 7 until the target value is found or the left pointer exceeds the right pointer.