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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int left,right,middle;
left=0,right=n-1;
while(left<=right){
middle=((right-left)>>1)+left;
if(A[middle]==target)
return true;
if(A[left]<A[middle]){//left is sorted
if(A[left]<=target && target<A[middle])
right=middle-1;
else
left=middle+1;
}else if(A[left]>A[middle]){//right is sorted
if(A[middle]<target && target<=A[right])
left=middle+1;
else
right=middle-1;
}
else
left++;
}
return false;
}
};