Problem Statement:
Find a number in a sorted rotated array.
Solution:
We need to find a number in a sorted rotataed array with complexity O (log N).
Here we will utilize the concept of binary search , which would modify in such a way that we achieve our target.
In case of a normal binary search only 2 cases exist, but here with sorted rotated array 4 cases comes up.
Depending on which part of the array is sorted we enter into that condition and see if the numeber is in that sorted array or not if not then it is definetly in the secord part, we continue this process until start is <= end.
Considering the above cases we can write a iterative Binary search search to find the element in a sorted rotated array.
Time Complexity : O( Log N)
Space Complexity : O(1)
Code :
Find a number in a sorted rotated array.
Solution:
We need to find a number in a sorted rotataed array with complexity O (log N).
Here we will utilize the concept of binary search , which would modify in such a way that we achieve our target.
In case of a normal binary search only 2 cases exist, but here with sorted rotated array 4 cases comes up.
Depending on which part of the array is sorted we enter into that condition and see if the numeber is in that sorted array or not if not then it is definetly in the secord part, we continue this process until start is <= end.
Considering the above cases we can write a iterative Binary search search to find the element in a sorted rotated array.
Time Complexity : O( Log N)
Space Complexity : O(1)
Code :
Please post comments and suggestions for further optimizing the code.
Happy Coding :)