I am pasting code here, I am unable to understand for which scenario is it failing, Please help me resolve it:
import java.util.*;
public class Main {
public static void main(String args[]) {
// Your Code Here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
int m = sc.nextInt();
int ans = binarySearch(0,arr.length-1,m,arr);
System.out.print(ans);
}
public static int binarySearch(int lo,int hi,int m,int[] arr){
int mid = 0;
while(lo<=hi){
mid = (lo+hi)/2;
if(arr[mid] == m){
return mid;
}else if(arr[lo]<=arr[mid]){
if(m>=arr[lo] && m<=arr[hi])
hi= mid-1;
else{
lo = mid+1;
}
}else{
if(m>=arr[mid] && m<=arr[hi])
lo = mid+1;
else{
hi=mid-1;
}
}
}
return -1;
}
}