From cf12cf81eddcce12758e164cbb51b108654a1549 Mon Sep 17 00:00:00 2001 From: ARES2525 <163795673+ARES2525@users.noreply.github.com> Date: Sun, 5 Oct 2025 14:03:18 +0530 Subject: [PATCH 1/3] add search 2D matrix solutions for both LeetCode variations --- .../thealgorithms/matrix/Search2DMatrix.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/main/java/com/thealgorithms/matrix/Search2DMatrix.java diff --git a/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java new file mode 100644 index 000000000000..36602089ea8f --- /dev/null +++ b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java @@ -0,0 +1,87 @@ +package com.thealgorithms.matrix; + +/** + * Two variations of searching in a 2D matrix + * + * Variation 1: + * - Each row is sorted left to right + * - Each column is sorted top to bottom + * + * Variation 2: + * - The matrix is a flattened sorted array + * - i.e., row 0 ends, row 1 continues (matrix is sorted as a 1D array) + * + * LeetCode: #74 and #240 + */ +public class Search2DMatrix { + + /** + * Variation 1: Search in a matrix where each row and each column is sorted + * Time: O(m + n) + * + * @param matrix input matrix + * @param target value to find + * @return true if found, false otherwise + */ + public static boolean searchMatrixSortedRowsAndCols(int[][] matrix, int target) { + int rows = matrix.length; + if (rows == 0) return false; + + int cols = matrix[0].length; + int row = 0, col = cols - 1; + + while (row < rows && col >= 0) { + int val = matrix[row][col]; + if (val == target) return true; + else if (val > target) col--; + else row++; + } + + return false; + } + + /** + * Variation 2: Search in a matrix that behaves like a flattened sorted array + * Time: O(log(m * n)) + * + * @param matrix input matrix + * @param target value to find + * @return true if found, false otherwise + */ + public static boolean searchMatrixFlattenedSorted(int[][] matrix, int target) { + int rows = matrix.length; + if (rows == 0) return false; + + int cols = matrix[0].length; + int left = 0, right = rows * cols - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; + int val = matrix[mid / cols][mid % cols]; + + if (val == target) return true; + else if (val < target) left = mid + 1; + else right = mid - 1; + } + + return false; + } + + // Example usage + public static void main(String[] args) { + int[][] matrix1 = { + {1, 4, 7, 11}, + {2, 5, 8, 12}, + {3, 6, 9, 16}, + {10, 13, 14, 17} + }; + System.out.println(searchMatrixSortedRowsAndCols(matrix1, 5)); // true + + int[][] matrix2 = { + {1, 3, 5, 7}, + {10, 11, 16, 20}, + {23, 30, 34, 50} + }; + System.out.println(searchMatrixFlattenedSorted(matrix2, 3)); // true + } +} From b1fe8bcd582a77e3de9f1e03a0d012ef7ee53d5e Mon Sep 17 00:00:00 2001 From: ARES2525 <163795673+ARES2525@users.noreply.github.com> Date: Sun, 5 Oct 2025 14:14:49 +0530 Subject: [PATCH 2/3] Add unit tests for Search2DMatrix implementations - Added JUnit tests covering both variations of 2D matrix search. - Tests validate correctness for typical inputs and edge cases. - Helps increase code coverage and ensures algorithm correctness. --- .../matrix/Search2DMatrixTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/test/java/com/thealgorithms/matrix/Search2DMatrixTest.java diff --git a/src/test/java/com/thealgorithms/matrix/Search2DMatrixTest.java b/src/test/java/com/thealgorithms/matrix/Search2DMatrixTest.java new file mode 100644 index 000000000000..5e1f8fa7d3d4 --- /dev/null +++ b/src/test/java/com/thealgorithms/matrix/Search2DMatrixTest.java @@ -0,0 +1,30 @@ +package com.thealgorithms.matrix; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class Search2DMatrixTest { + + @Test + public void testSearchMatrixSortedRowsAndCols() { + int[][] matrix = { + {1, 4, 7, 11}, + {2, 5, 8, 12}, + {3, 6, 9, 16}, + {10, 13, 14, 17} + }; + assertTrue(Search2DMatrix.searchMatrixSortedRowsAndCols(matrix, 5)); + assertFalse(Search2DMatrix.searchMatrixSortedRowsAndCols(matrix, 20)); + } + + @Test + public void testSearchMatrixFlattenedSorted() { + int[][] matrix = { + {1, 3, 5, 7}, + {10, 11, 16, 20}, + {23, 30, 34, 50} + }; + assertTrue(Search2DMatrix.searchMatrixFlattenedSorted(matrix, 3)); + assertFalse(Search2DMatrix.searchMatrixFlattenedSorted(matrix, 4)); + } +} From 4b2e5ad396cbada0a807af33ca8d5e825fa20675 Mon Sep 17 00:00:00 2001 From: ARES2525 <163795673+ARES2525@users.noreply.github.com> Date: Sun, 5 Oct 2025 14:16:52 +0530 Subject: [PATCH 3/3] Search2DMatrix.java --- .../thealgorithms/matrix/Search2DMatrix.java | 82 +++++++++---------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java index 36602089ea8f..2eb1c99a7025 100644 --- a/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java +++ b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java @@ -2,86 +2,82 @@ /** * Two variations of searching in a 2D matrix - * + * * Variation 1: * - Each row is sorted left to right * - Each column is sorted top to bottom - * + * * Variation 2: * - The matrix is a flattened sorted array * - i.e., row 0 ends, row 1 continues (matrix is sorted as a 1D array) - * + * * LeetCode: #74 and #240 */ public class Search2DMatrix { /** - * Variation 1: Search in a matrix where each row and each column is sorted - * Time: O(m + n) + * Search in a 2D matrix where rows and columns are sorted. * - * @param matrix input matrix - * @param target value to find - * @return true if found, false otherwise + * @param matrix The 2D matrix with sorted rows and columns. + * @param target The target integer to search for. + * @return true if target is found, otherwise false. */ public static boolean searchMatrixSortedRowsAndCols(int[][] matrix, int target) { - int rows = matrix.length; - if (rows == 0) return false; + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return false; + } + int rows = matrix.length; int cols = matrix[0].length; - int row = 0, col = cols - 1; + + int row = 0; + int col = cols - 1; while (row < rows && col >= 0) { int val = matrix[row][col]; - if (val == target) return true; - else if (val > target) col--; - else row++; + if (val == target) { + return true; + } else if (val > target) { + col--; + } else { + row++; + } } return false; } /** - * Variation 2: Search in a matrix that behaves like a flattened sorted array - * Time: O(log(m * n)) + * Search in a 2D matrix that is sorted as if flattened into a 1D array. * - * @param matrix input matrix - * @param target value to find - * @return true if found, false otherwise + * @param matrix The 2D matrix. + * @param target The target integer to search for. + * @return true if target is found, otherwise false. */ public static boolean searchMatrixFlattenedSorted(int[][] matrix, int target) { - int rows = matrix.length; - if (rows == 0) return false; + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return false; + } + int rows = matrix.length; int cols = matrix[0].length; - int left = 0, right = rows * cols - 1; + + int left = 0; + int right = rows * cols - 1; while (left <= right) { int mid = left + (right - left) / 2; int val = matrix[mid / cols][mid % cols]; - if (val == target) return true; - else if (val < target) left = mid + 1; - else right = mid - 1; + if (val == target) { + return true; + } else if (val < target) { + left = mid + 1; + } else { + right = mid - 1; + } } return false; } - - // Example usage - public static void main(String[] args) { - int[][] matrix1 = { - {1, 4, 7, 11}, - {2, 5, 8, 12}, - {3, 6, 9, 16}, - {10, 13, 14, 17} - }; - System.out.println(searchMatrixSortedRowsAndCols(matrix1, 5)); // true - - int[][] matrix2 = { - {1, 3, 5, 7}, - {10, 11, 16, 20}, - {23, 30, 34, 50} - }; - System.out.println(searchMatrixFlattenedSorted(matrix2, 3)); // true - } }