From d70aecbf109426e495ec3921f75234ef3948f301 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Thu, 22 Oct 2026 12:33:46 +0530 Subject: [PATCH 1/2] fix(searches): support rectangular matrices and null safety in SearchInARowAndColWiseSortedMatrix --- .../SearchInARowAndColWiseSortedMatrix.java | 74 +++++++++++-------- ...earchInARowAndColWiseSortedMatrixTest.java | 45 +++++++++++ 2 files changed, 87 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java index b53c7e5256ca..20a07adafd26 100644 --- a/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java +++ b/src/main/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrix.java @@ -1,32 +1,42 @@ -package com.thealgorithms.searches; - -public class SearchInARowAndColWiseSortedMatrix { - /** - * Search a key in row and column wise sorted matrix - * - * @param matrix matrix to be searched - * @param value Key being searched for - * @author Sadiul Hakim : https://github.com/sadiul-hakim - */ - public int[] search(int[][] matrix, int value) { - int n = matrix.length; - // This variable iterates over rows - int i = 0; - // This variable iterates over columns - int j = n - 1; - int[] result = {-1, -1}; - while (i < n && j >= 0) { - if (matrix[i][j] == value) { - result[0] = i; - result[1] = j; - return result; - } - if (value > matrix[i][j]) { - i++; - } else { - j--; - } - } - return result; - } -} +package com.thealgorithms.searches; + +public final class SearchInARowAndColWiseSortedMatrix { + /** + * Search a key in row and column wise sorted matrix + * + * @param matrix matrix to be searched + * @param value Key being searched for + * @return row and column indices if found, otherwise {-1, -1} + * @author Sadiul Hakim : https://github.com/sadiul-hakim + */ + public int[] search(int[][] matrix, int value) { + if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) { + return new int[] {-1, -1}; + } + + int rows = matrix.length; + int cols = matrix[0].length; + // This variable iterates over rows + int i = 0; + // This variable iterates over columns, starting from top-right corner + int j = cols - 1; + int[] result = {-1, -1}; + while (i < rows && j >= 0) { + if (matrix[i] == null || j >= matrix[i].length) { + i++; + continue; + } + if (matrix[i][j] == value) { + result[0] = i; + result[1] = j; + return result; + } + if (value > matrix[i][j]) { + i++; + } else { + j--; + } + } + return result; + } +} diff --git a/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java b/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java index 88227804b775..41717b9d1fa7 100644 --- a/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java +++ b/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java @@ -63,4 +63,49 @@ void testSearchInColWiseSortedMatrix() { int[] expected = {1, 1}; // Found at (1, 1) assertArrayEquals(expected, searcher.search(matrix, value), "Value should be found in the column-wise sorted matrix"); } + + @Test + void testSearchInNullMatrix() { + assertArrayEquals(new int[] {-1, -1}, searcher.search(null, 5), "Should return {-1, -1} for null matrix"); + } + + @Test + void testSearchInMatrixWithEmptyRows() { + int[][] matrix = {{}}; + assertArrayEquals(new int[] {-1, -1}, searcher.search(matrix, 5), "Should return {-1, -1} for matrix with empty first row"); + } + + @Test + void testSearchInRectangularMatrixMoreColsThanRowsFound() { + int[][] matrix = { + {1, 3, 5, 7, 9}, + {2, 4, 6, 8, 10} + }; + int[] expected = {0, 4}; // Value 9 is at row 0, col 4 + assertArrayEquals(expected, searcher.search(matrix, 9), "Value 9 should be found at row 0, col 4"); + } + + @Test + void testSearchInRectangularMatrixMoreRowsThanColsFound() { + int[][] matrix = { + {1, 5}, + {2, 6}, + {3, 7}, + {4, 8} + }; + int[] expected = {2, 0}; // Value 3 is at row 2, col 0 + assertArrayEquals(expected, searcher.search(matrix, 3), "Value 3 should be found at row 2, col 0"); + } + + @Test + void testSearchInRectangularMatrixNotFound() { + int[][] matrix = { + {1, 3, 5}, + {2, 4, 6}, + {7, 8, 9}, + {10, 11, 12} + }; + int[] expected = {-1, -1}; + assertArrayEquals(expected, searcher.search(matrix, 15), "Value 15 should not be found"); + } } From 4023532f0f60f584f1f8440e31107d8256fc610a Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Thu, 22 Oct 2026 12:55:44 +0530 Subject: [PATCH 2/2] style: format SearchInARowAndColWiseSortedMatrixTest with clang-format --- ...earchInARowAndColWiseSortedMatrixTest.java | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java b/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java index 41717b9d1fa7..e37eec82cb8e 100644 --- a/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java +++ b/src/test/java/com/thealgorithms/searches/SearchInARowAndColWiseSortedMatrixTest.java @@ -77,34 +77,21 @@ void testSearchInMatrixWithEmptyRows() { @Test void testSearchInRectangularMatrixMoreColsThanRowsFound() { - int[][] matrix = { - {1, 3, 5, 7, 9}, - {2, 4, 6, 8, 10} - }; + int[][] matrix = {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}; int[] expected = {0, 4}; // Value 9 is at row 0, col 4 assertArrayEquals(expected, searcher.search(matrix, 9), "Value 9 should be found at row 0, col 4"); } @Test void testSearchInRectangularMatrixMoreRowsThanColsFound() { - int[][] matrix = { - {1, 5}, - {2, 6}, - {3, 7}, - {4, 8} - }; + int[][] matrix = {{1, 5}, {2, 6}, {3, 7}, {4, 8}}; int[] expected = {2, 0}; // Value 3 is at row 2, col 0 assertArrayEquals(expected, searcher.search(matrix, 3), "Value 3 should be found at row 2, col 0"); } @Test void testSearchInRectangularMatrixNotFound() { - int[][] matrix = { - {1, 3, 5}, - {2, 4, 6}, - {7, 8, 9}, - {10, 11, 12} - }; + int[][] matrix = {{1, 3, 5}, {2, 4, 6}, {7, 8, 9}, {10, 11, 12}}; int[] expected = {-1, -1}; assertArrayEquals(expected, searcher.search(matrix, 15), "Value 15 should not be found"); }