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.
bool Find(int A[], int first, int last,int target )
{
int mid = (first + last) / 2;
int leftbound = mid;
int rightbound = mid;
while (A[leftbound] == A[mid] && leftbound >= first)
--leftbound;
while (A[rightbound] == A[mid] && rightbound <= last)
++rightbound;
if (A[mid] == target)
return true;
else if (leftbound<first)
{
if (rightbound>last)
return false;
else
return Find(A, rightbound,last,target);
}
else
{
if (rightbound > last)
return Find(A, first, leftbound, target);
else
return Find(A, rightbound, last, target) || Find(A, first, leftbound, target);
}
}
bool search(int A[], int n, int target) {
return Find(A, 0, n - 1, target);
}