Matrix search 1 out of three test case is failed

public class SearchInMatrix {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner s =  new Scanner(System.in);
	int m = s.nextInt();
	int n = s.nextInt();
	int[][] arr = new int[m][n];
	// taking input
	for(int i=0; i<m; i++) {
		for(int j=0; j<n; j++) {
			arr[i][j] = s.nextInt();
		}
	} // end
	int k = s.nextInt();
	System.out.println(searchMatrix(arr, m, n, k));
	s.close();
}

private static int searchMatrix(int[][] arr, int m, int n, int k) {
	// TODO Auto-generated method stub
	int l=0, r=m-1;
	while(l <= r) {
		int mid = l + (r-l)/2;
		if(k >= arr[mid][0] && k <= arr[mid][n-1]) {
			return binarySearch(arr[mid], n,k);
		}
		if(k < arr[mid][0]) {
			r = mid-1;
		}
		else {
			l = mid+1;
		}
	}
	return 0;
}

private static int binarySearch(int[] arr, int n, int k) {
	// TODO Auto-generated method stub
	int l=0, r=n-1;
	while(l <= r) {
		int mid = l + (r-l)/2;
		if(arr[mid] == k) {
			return 1;
		}
		if(k < arr[mid]) {
			r = mid -1;
		}else {
			l = mid +1;
		}
	}
	return 0;
}

}

Hey @danyalkhan8271 For the input :
4 4
10 20 30 40 15 25 35 45 27 29 37 48 32 33 39 50
29
correct : 1
Your output : 0

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.