Joe
BAN USER
Comments (3)
Reputation 50
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
4
of 4 vote
We can do this using the binary search approach as following:
int rotatedSorted(int[] arr, int target){
int left = 0;
int right = arr.length-1;
while(left<=right){
int mid = left+(right-left)/2;
if(arr[mid]==target)
return arr[mid]
if(arr[left]<=arr[mid]){
if(arr[left]<=target && target<arr[mid]){
right=mid-1;
}else{
left=mid+1;
}
}else{
if(arr[mid]<target && target <= arr[right]){
left=mid+1;
}else{
right=mid-1;
}
}
}
return -1;
}
Comment hidden because of low score. Click to expand.
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
I don't understand the question. Why are {2} and {4} are allowed as subsets and not {5} and {7} ? 5 and 7 are also less than 8.
- Joe February 27, 2019