Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,
0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
In each iteration of binary search, at least one side should be correctly ordered. If the target located in that side, we can eliminate another side. Otherwise we can eliminate the ordered side.Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array.
The only difference is, we need to consider the scenario where A[m] == A[e]. The simplest approach is to reduce e by 1.
Comments
Post a Comment