binary-Search array--

what is error in my code–

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt(), arr[] = new int[n];
for(int i = 0;i<arr.length;i++){
arr[i] = cin.nextInt();
}
int m = cin.nextInt();
int res = binarySearch(arr,0,arr.length-1,m);
System.out.println(res);
}
public static int binarySearch(int []arr,int l,int r,int key){
if(l>r){
// System.out.println(“Element not found”);
return -1;
}
int mid = l+r/2;
while(l<=r){
if(arr[mid]>key){
r = mid-1;
}else if(arr[mid] == key){
return mid;
}else{
l = mid+1;
}
binarySearch(arr,l,r,key);
}
return -1;
}
}

HI @AbhishekAhlawat1102,
Your code has a few errors. I have corrected them .Look for comments in below program.
While loop that you used was not required, your mid function will work only if l=0 and you should return binarySearch recursive call.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.