i have this question where im given a an array that contains the distance of a traveler from a certain destination at the i-th time of the day. ideally the array should be in descending order but the array could contain mistakes where the traveler gets further from the destenation and i should find at which index does that mistake happen using binary search. heres the code i wrote:
int find_mistake(int dist[] , int n){
int l = 0 , r = n-1 , mid;
while(l<=r){
mid = (l+r)/2;
if (dist[mid] > dist[mid - 1]){
return mid;
}
else if (dist[mid] > dist[l]){
r = mid ;
}
else{
l = mid ;
}
}
return -1;
i would be glad if someone pointed mistakes in it as ive seen other solution and im not sure about mine.