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.
int BinarySearch2(int *arr,int len,int target)
{
int first = 0,last = len,mid = 0;
while(first <= last)
{
mid = (last - first)/2 + first;
if(arr[mid] == target) return mid;
if(arr[mid] > arr[last - 1])
{
if(target >= arr[first] && target < arr[mid])
last = mid;
else
first = mid + 1;
}
else if(arr[mid] == arr[last - 1]) last--;
else
{
if(target <= arr[last - 1] && target > arr[mid])
first = mid + 1;
else
last = mid;
}
}
return -1;
}