Search in Rotated Sorted Array
We are now solving a problem which is asked by many of the product based companies. They always want you to give a proper solution with all edge cases covered.However, an array sorted in ascending is rotated (i.e., [0,1,2,3,4,5,6,7] might become [4,5,6,7,0,1,2,3]).
Constraints
Example 1:
Input: nums = [4,5,6,7,0,1,2], searchElement = 0
Output: 4
Linear Search Solution- O(n) time complexity
It comes to our mind easily to check each element of an array until it finds it or till the array reaches the end of the index.
Binary Search Solution- O(log n) time complexity
Constraints
- No duplicate element in an array.
- Runtime complexity must be in the order of O(log n).
- If found then return index otherwise return -1.
Example 1:
Input: nums = [4,5,6,7,0,1,2], searchElement = 0
Output: 4
Linear Search Solution- O(n) time complexity
It comes to our mind easily to check each element of an array until it finds it or till the array reaches the end of the index.
class Solution { public static void main(String[] args) { System.out.println(search(new int[]{4,5,6,7,0,1,2}, 0)); } public static int search(int[] nums, int searchElement) { if (nums.length != 0) { for (int i = 0; i < nums.length; i++) { if (nums[i] == searchElement) return i; } } return -1; } }
Binary Search Solution- O(log n) time complexity
- We have to look for the base case where the recursion will terminate.
- We use the divide and conquer strategy to breakdown the problem into the same sub-problem until we get the result.
- Calculate the middle index using (left + right index)/2 and check if the middle index of an array is equal to the search element. If yes, return the middle index, otherwise, go to the next step.
- If left index s less than or equal to the middle element is it means the left subarray is sorted, otherwise the right subarray is sorted.
- If the left subarray is sorted and a search element exists in the range of it then update the right index otherwise update the left index.
- If the right subarray sorted and search element exists in the range of it then update the left index otherwise update the right index.
- Repeat the same step until the loop terminates with a base condition or has found the element.
class Solution { public static void main(String[] args) { System.out.println(search(new int[]{1,3}, 3)); } public static int search(int[] nums, int searchElement) { return search(nums, searchElement, 0, nums.length-1); } public static int search(int[] nums, int searchElement, int leftIndex, int rightIndex) { // Base case where we don't have the search element in the array
if (leftIndex > rightIndex) { return -1; } // Mid element of the array.
int mid = leftIndex + ((rightIndex-leftIndex)/2);
// Find the element and returning the index value.
if (nums[mid] == searchElement) return mid; // It will ensure that the left side from mid is sorted array.
if (nums[leftIndex] <= nums[mid]) { if (nums[leftIndex] <= searchElement && searchElement <= nums[mid]) { rightIndex = mid; } else { leftIndex = mid +1; } }
// It will ensure that the right side from mid is sorted array.
else { // It will check the search element does exist in right sub sorted array
if (nums[rightIndex] >= searchElement && searchElement >= nums[mid]) leftIndex = mid; else
rightIndex = mid -1; } return search(nums, searchElement, leftIndex, rightIndex); } }I hope you are understanding the solution to the given problem. We will come up with a new challenge soon.
Comments
Post a Comment