From e638b4218d65e50910ed43012219fc078c47668b Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Mon, 2 Nov 2026 11:30:03 +0530 Subject: [PATCH] fix(searches): add null safety and empty matrix handling to RowColumnWiseSorted2dArrayBinarySearch --- ...owColumnWiseSorted2dArrayBinarySearch.java | 11 +++- ...lumnWiseSorted2dArrayBinarySearchTest.java | 56 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java b/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java index ecdcd36adf21..940d92d9c382 100644 --- a/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearch.java @@ -20,7 +20,7 @@ * doesn't exist in the array, an array * {-1, -1} will be returned. */ -public class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgorithm { +public final class RowColumnWiseSorted2dArrayBinarySearch implements MatrixSearchAlgorithm { @Override public > int[] find(T[][] matrix, T key) { @@ -28,10 +28,19 @@ public > int[] find(T[][] matrix, T key) { } public static > int[] search(T[][] matrix, T target) { + if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0 || target == null) { + return new int[] {-1, -1}; + } + int rowPointer = 0; // The pointer at 0th row int colPointer = matrix[0].length - 1; // The pointer at end column while (rowPointer < matrix.length && colPointer >= 0) { + if (matrix[rowPointer] == null || colPointer >= matrix[rowPointer].length) { + rowPointer++; + continue; + } + int comp = target.compareTo(matrix[rowPointer][colPointer]); if (comp == 0) { diff --git a/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java index 8d1423cbeeb0..9259f57941a7 100644 --- a/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/RowColumnWiseSorted2dArrayBinarySearchTest.java @@ -237,4 +237,60 @@ public void rowColumnSorted2dArrayBinarySearchTestTallMatrixNotFound() { assertEquals(expected[0], ans[0]); assertEquals(expected[1], ans[1]); } + + @Test + public void testNullMatrixReturnsNotFound() { + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(null, 5); + int[] expected = {-1, -1}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } + + @Test + public void testEmptyMatrixReturnsNotFound() { + Integer[][] matrix = {}; + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 5); + int[] expected = {-1, -1}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } + + @Test + public void testEmptyFirstRowReturnsNotFound() { + Integer[][] matrix = {{}}; + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 5); + int[] expected = {-1, -1}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } + + @Test + public void testNullTargetReturnsNotFound() { + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(WIDE_RECTANGULAR_MATRIX, null); + int[] expected = {-1, -1}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } + + @Test + public void testMatrixWithNullRow() { + Integer[][] matrix = { + {10, 20, 30}, + null, + {18, 28, 38}, + }; + int[] ans = RowColumnWiseSorted2dArrayBinarySearch.search(matrix, 28); + int[] expected = {2, 1}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } + + @Test + public void testFindMethodThroughInterface() { + RowColumnWiseSorted2dArrayBinarySearch searcher = new RowColumnWiseSorted2dArrayBinarySearch(); + int[] ans = searcher.find(WIDE_RECTANGULAR_MATRIX, 35); + int[] expected = {1, 2}; + assertEquals(expected[0], ans[0]); + assertEquals(expected[1], ans[1]); + } }