-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion38.java
More file actions
38 lines (30 loc) · 905 Bytes
/
question38.java
File metadata and controls
38 lines (30 loc) · 905 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
28
29
30
31
32
33
34
35
36
37
38
public class question38 {
public static void main(String[] args) {
int[][] matrix = {
{10, 20, 30, 40},
{40, 50, 60, 70},
{27, 29, 28, 37},
{32, 33, 39, 66}
};
int target = 20;
int rows = matrix.length;
int cols = matrix[0].length;
int i = 0, j = cols-1;
boolean found = false;
while (i < rows && j >= 0){
if (matrix[i][j] == target)
{
System.out.println("Found at ("+ i + ", "+ j + ")");
found = true;
break;
}else if(matrix[i][j] > target){
j--;
} else {
i++;
}
}
if(!found){
System.out.println("Not Found");
}
}
}