Tle error and run error for same code but with different variable name

import java.util.*;
public class Main {
public static void main(String args[]) {
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();
	System.out.println(binary(arr,m));
}	

public static int binary(int[] arr, int m) {
	int li=0;
	int hi=arr.length-1;
	
	while(li<=hi){
		int mid=li+hi/2;
		
		 if (arr[mid] < m) {
              li = mid + 1;
          } else if (arr[mid] > m) {
              hi= mid - 1;
          } else {
              return mid;
          }
	}
	return -1;
}	

}

I wrote the same code but changed the array name to arr to a than it passed all the test case

Hey @ashutosh2,
You have an error in the calculation of mid.

Here, it will perform the division operation before summing the 2 indexes.
The rest of the logic is good and it will work.
Here’s your code.

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.