Skip to content

Commit 96ed2d7

Browse files
fix(searches): support rectangular matrices and null safety in SearchInARowAndColWiseSortedMatrix (#7655)
* fix(searches): support rectangular matrices and null safety in SearchInARowAndColWiseSortedMatrix * style: format SearchInARowAndColWiseSortedMatrixTest with clang-format
1 parent 976cebc commit 96ed2d7

2 files changed

Lines changed: 74 additions & 32 deletions

File tree

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
1-
package com.thealgorithms.searches;
2-
3-
public class SearchInARowAndColWiseSortedMatrix {
4-
/**
5-
* Search a key in row and column wise sorted matrix
6-
*
7-
* @param matrix matrix to be searched
8-
* @param value Key being searched for
9-
* @author Sadiul Hakim : https://github.com/sadiul-hakim
10-
*/
11-
public int[] search(int[][] matrix, int value) {
12-
int n = matrix.length;
13-
// This variable iterates over rows
14-
int i = 0;
15-
// This variable iterates over columns
16-
int j = n - 1;
17-
int[] result = {-1, -1};
18-
while (i < n && j >= 0) {
19-
if (matrix[i][j] == value) {
20-
result[0] = i;
21-
result[1] = j;
22-
return result;
23-
}
24-
if (value > matrix[i][j]) {
25-
i++;
26-
} else {
27-
j--;
28-
}
29-
}
30-
return result;
31-
}
32-
}
1+
package com.thealgorithms.searches;
2+
3+
public final class SearchInARowAndColWiseSortedMatrix {
4+
/**
5+
* Search a key in row and column wise sorted matrix
6+
*
7+
* @param matrix matrix to be searched
8+
* @param value Key being searched for
9+
* @return row and column indices if found, otherwise {-1, -1}
10+
* @author Sadiul Hakim : https://github.com/sadiul-hakim
11+
*/
12+
public int[] search(int[][] matrix, int value) {
13+
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
14+
return new int[] {-1, -1};
15+
}
16+
17+
int rows = matrix.length;
18+
int cols = matrix[0].length;
19+
// This variable iterates over rows
20+
int i = 0;
21+
// This variable iterates over columns, starting from top-right corner
22+
int j = cols - 1;
23+
int[] result = {-1, -1};
24+
while (i < rows && j >= 0) {
25+
if (matrix[i] == null || j >= matrix[i].length) {
26+
i++;
27+
continue;
28+
}
29+
if (matrix[i][j] == value) {
30+
result[0] = i;
31+
result[1] = j;
32+
return result;
33+
}
34+
if (value > matrix[i][j]) {
35+
i++;
36+
} else {
37+
j--;
38+
}
39+
}
40+
return result;
41+
}
42+
}

‎src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,36 @@ void testSearchInColWiseSortedMatrix() {
6363
int[] expected = {1, 1}; // Found at (1, 1)
6464
assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in the column-wise sorted matrix");
6565
}
66+
67+
@Test
68+
void testSearchInNullMatrix() {
69+
assertArrayEquals(new int[] {-1, -1}, searcher.search(null, 5), "Should return {-1, -1} for null matrix");
70+
}
71+
72+
@Test
73+
void testSearchInMatrixWithEmptyRows() {
74+
int[][] matrix = {{}};
75+
assertArrayEquals(new int[] {-1, -1}, searcher.search(matrix, 5), "Should return {-1, -1} for matrix with empty first row");
76+
}
77+
78+
@Test
79+
void testSearchInRectangularMatrixMoreColsThanRowsFound() {
80+
int[][] matrix = {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}};
81+
int[] expected = {0, 4}; // Value 9 is at row 0, col 4
82+
assertArrayEquals(expected, searcher.search(matrix, 9), "Value 9 should be found at row 0, col 4");
83+
}
84+
85+
@Test
86+
void testSearchInRectangularMatrixMoreRowsThanColsFound() {
87+
int[][] matrix = {{1, 5}, {2, 6}, {3, 7}, {4, 8}};
88+
int[] expected = {2, 0}; // Value 3 is at row 2, col 0
89+
assertArrayEquals(expected, searcher.search(matrix, 3), "Value 3 should be found at row 2, col 0");
90+
}
91+
92+
@Test
93+
void testSearchInRectangularMatrixNotFound() {
94+
int[][] matrix = {{1, 3, 5}, {2, 4, 6}, {7, 8, 9}, {10, 11, 12}};
95+
int[] expected = {-1, -1};
96+
assertArrayEquals(expected, searcher.search(matrix, 15), "Value 15 should not be found");
97+
}
6698
}

0 commit comments

Comments
 (0)