Need a help in my solution


#1

int Solution::search(const vector &A, int B) {
int n=A.size();
int start=0;
int end=n-1;
if(n==1){
if(A[0]==B){
return A[0];
}
else return -1;
}
if(A[0]<A[n-1]){//means array is sorted
while(start<end){
int mid=(start+end)/2;
if(A[mid]==B){
return mid;
}
if(A[mid]<B){
start=mid+1;
}
else end=mid-1;
}
return -1;
}
while(start<end){
int mid=(start+end)/2;
if(A[mid]>A[end]){
start=mid+1;
}
else{
end=mid;
}
}
int newstart=start;
if(A[newstart]<B && B>A[n-1]){
int s=0;
int e=newstart-1;
while(s<e){
int m=(s+e)/2;
if(A[m]==B){
return m;
}
if(A[m]<B){
s=m+1;
}
else e=m-1;
}
return -1;
}
if(A[newstart]==B){
return newstart;
}
if(A[newstart]<B && B<=A[n-1]){
int e=n-1;
int s=newstart;
while(s<e){
int m=(s+e)/2;
if(A[m]==B){
return m;
}
if(A[m]<B){
s=m+1;
}
else e=m-1;
}
return -1;
}

}