-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathsearchMatrix2.java
52 lines (44 loc) Β· 1.18 KB
/
searchMatrix2.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package Lecture14;
public class searchMatrix2 {
public static void main(String[] args) {
int[][] arr = { { 10, 20, 30, 40 },
{ 11, 21, 31, 41 },
{ 12, 22, 32, 42 },
{ 13, 23, 33, 43 } };
System.out.println(searchKeyMatrix(arr, 22)); // true
System.out.println(searchKeyMatrix(arr, 10)); // true
System.out.println(searchKeyMatrix(arr, 42)); // true
System.out.println(searchKeyMatrix(arr, 23)); // true
System.out.println(searchKeyMatrix(arr, 13)); // true
System.out.println(searchKeyMatrix(arr, 2)); // false
}
public static boolean searchKeyMatrix(int[][] arr, int key) {
int start = 0, end = arr.length - 1;
int mid = (start + end) / 2;
if (arr[0][mid] == key || arr[mid][0] == key) {
return true;
}
int startcol = 0;
int endcol = arr.length - 1;
if (key < arr[0][mid]) {
endcol = mid;
} else {
startcol = mid;
}
int startrow = 0;
int endrow = arr.length - 1;
if (key < arr[mid][0]) {
endrow = mid;
} else {
startrow = mid;
}
for (int row = startrow; row <= endrow; row++) {
for (int col = startcol; col <= endcol; col++) {
if (arr[row][col] == key) {
return true;
}
}
}
return false;
}
}