1
1
package com .thealgorithms .searches ;
2
2
3
+ import java .util .Arrays ;
4
+
3
5
public class SearchInARowAndColWiseSortedMatrix {
4
6
/**
5
7
* Search a key in row and column wise sorted matrix
@@ -9,18 +11,20 @@ public class SearchInARowAndColWiseSortedMatrix {
9
11
* @author Sadiul Hakim : https://github.com/sadiul-hakim
10
12
*/
11
13
12
- public void search (int [][] matrix , int value ) {
14
+ public int [] search (int [][] matrix , int value ) {
13
15
int n = matrix .length ;
14
16
// This variable iterates over rows
15
17
int i = 0 ;
16
18
// This variable iterates over columns
17
19
int j = n - 1 ;
20
+ int [] result = { -1 , -1 };
18
21
19
22
while (i < n && j >= 0 ) {
20
23
21
24
if (matrix [i ][j ] == value ) {
22
- System .out .println (value + " found at position row : " + i + " ,column :" + j );
23
- return ;
25
+ result [0 ] = i ;
26
+ result [1 ] = j ;
27
+ return result ;
24
28
}
25
29
if (value > matrix [i ][j ]) {
26
30
@@ -31,7 +35,7 @@ public void search(int[][] matrix, int value) {
31
35
}
32
36
33
37
}
34
- System . out . println ( value + "Not Found." ) ;
38
+ return result ;
35
39
}
36
40
37
41
public static void main (String [] args ) {
@@ -44,6 +48,7 @@ public static void main(String[] args) {
44
48
};
45
49
46
50
var search = new SearchInARowAndColWiseSortedMatrix ();
47
- search .search (matrix , 26 );
51
+ int [] res = search .search (matrix , 26 );
52
+ System .out .println (Arrays .toString (res ));
48
53
}
49
54
}
0 commit comments