There's one run error in test case 3

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 <n && r>=0) {
		if(arr[l][r] == k ) {
			return 1;
		}
		if(arr[l][r] > k) {
			r--;
		}
		else {
			l++;
		}
	}
	return 0;
}

Hey @danyalkhan8271 Your code is failing for the testcase :
1 6
18 21 27 38 55 67
55
bcoz you have used ‘l’ a parameter for row and according to your logic when ever you increment ‘l’ is becomes 1 and since there’s only 1 row so 1 index will be out of bound.

thankyou for your quick response

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.