Untitled
unknown
plain_text
2 years ago
676 B
11
Indexable
class Solution {
public:
int search(vector<int>& arr, int t) { //hald of the vector is sorted and half isnt
int l=0,h=arr.size()-1;
while(l<=h){
int mid = l+(h-l)/2;
if(arr[mid]==t) return mid;
if(arr[l] <= arr[mid]){ //left sorted
if(arr[l]<=t && t<=arr[mid]){
h=mid-1;
}else{
l=mid+1;
}
}
else{ //right sorted
if(t>=arr[mid] && t<=arr[h]){
l=mid+1;
}else{
h=mid-1;
}
}
}
return -1;
}
};Editor is loading...