Find the error in this


#1

its not the optimised solution but giving wrong output.
int findit(vector &A,int num){
int start=0,end=A.size()-1;
int mid;
int ans1,ans2;
while(start <= end){
mid = start + (end-start)/2;
if(A[mid] == num){
return A[mid];
}
else if(A[mid] > num){
ans2 = A[mid];
end = mid-1;
}
else{
ans1 = A[mid];
start = mid+1;
}
}
if(abs(ans1-num) > abs(ans2-num)){
return ans2;
}
return ans1;
}

int Solution::solve(vector &A, vector &B, vector &C) {
int maxi=INT_MIN;
int a,b,c;
int ans = INT_MAX;
for(int i=0;i<A.size();i++){
maxi = A[i];
a = findit(A,maxi);
b = findit(B,maxi);
c = findit(C,maxi);
ans = min(ans,max({a,b,c})-min({a,b,c}));
}
return ans;
}