11package com .thealgorithms .searches ;
22
3+ import java .util .Arrays ;
4+
35public class SearchInARowAndColWiseSortedMatrix {
46 /**
57 * Search a key in row and column wise sorted matrix
@@ -9,18 +11,20 @@ public class SearchInARowAndColWiseSortedMatrix {
911 * @author Sadiul Hakim : https://github.com/sadiul-hakim
1012 */
1113
12- public void search (int [][] matrix , int value ) {
14+ public int [] search (int [][] matrix , int value ) {
1315 int n = matrix .length ;
1416 // This variable iterates over rows
1517 int i = 0 ;
1618 // This variable iterates over columns
1719 int j = n - 1 ;
20+ int [] result = { -1 , -1 };
1821
1922 while (i < n && j >= 0 ) {
2023
2124 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 ;
2428 }
2529 if (value > matrix [i ][j ]) {
2630
@@ -31,7 +35,7 @@ public void search(int[][] matrix, int value) {
3135 }
3236
3337 }
34- System . out . println ( value + "Not Found." ) ;
38+ return result ;
3539 }
3640
3741 public static void main (String [] args ) {
@@ -44,6 +48,7 @@ public static void main(String[] args) {
4448 };
4549
4650 var search = new SearchInARowAndColWiseSortedMatrix ();
47- search .search (matrix , 26 );
51+ int [] res = search .search (matrix , 26 );
52+ System .out .println (Arrays .toString (res ));
4853 }
4954}
0 commit comments