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.
class Solution {
public:
bool search(int A[], int n, int target) {
int start = 0, end = n - 1;
while( start <= end){
int mid = ( start + end) >> 1;
if( A[mid] == target){
return true;
}
if( A[start] < A[mid]){
if( A[mid] > target && target >= A[start])
end = mid - 1;
else
start = mid + 1;
}
else if( A[start] > A[mid]){
if( A[mid] < target && target <= A[end])
start = mid + 1;
else
end = mid - 1;
}
else
++start;
}
return false;
}
};