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..2eb1c99a7025 --- /dev/null +++ b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java @@ -0,0 +1,83 @@ +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 { + + /** + * Search in a 2D matrix where rows and columns are sorted. + * + * @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) { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return false; + } + + int rows = matrix.length; + int cols = matrix[0].length; + + 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++; + } + } + + return false; + } + + /** + * Search in a 2D matrix that is sorted as if flattened into a 1D array. + * + * @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) { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return false; + } + + int rows = matrix.length; + int cols = matrix[0].length; + + 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; + } + } + + return false; + } +} 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)); + } +}