forked from dharmanshu1921/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchInMatrix.java
More file actions
27 lines (24 loc) · 996 Bytes
/
BinarySearchInMatrix.java
File metadata and controls
27 lines (24 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class BinarySearchInMatrix {
public String searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
return "No";
}
int column = matrix[0].length - 1;
int row = 0;
while (column >= 0 && row <= matrix.length - 1) {
if (target == matrix[row][column]) {
return "Yes";
} else if (target < matrix[row][column]) {
column--;
} else if (target > matrix[row][column]) {
row++;
}
}
return "No";
}
public static void main(String[] args) {
BinarySearchInMatrix a = new BinarySearchInMatrix();
System.out.println("The Target 5 is Found in Given Matrix ?" + a.searchMatrix(new int[][] { { 1, 4, 7, 11, 15 },
{ 2, 5, 8, 12, 19 }, { 3, 6, 9, 16, 22 }, { 10, 13, 14, 17, 24 }, { 18, 21, 23, 26, 30 } }, 5));
}
}