From 35f3259b6d59527c5711058d74e5c9b5db616f1b Mon Sep 17 00:00:00 2001 From: AHVSSATHVIK Date: Tue, 25 Nov 2025 06:52:25 +0000 Subject: [PATCH 1/4] Add Search a 2D Matrix algorithm --- .../thealgorithms/matrix/Search2DMatrix.java | 47 +++++++++++++++++++ 1 file changed, 47 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..e7b642e93767 --- /dev/null +++ b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java @@ -0,0 +1,47 @@ +package com.thealgorithms.matrix; + +/** + * Search a 2D Matrix + * Each row is sorted left to right, and the first element of each row + * is greater than the last element of the previous row. + * + * Time Complexity: O(log(m*n)) + * Space Complexity: O(1) + */ +public class Search2DMatrix { + + /** + * Searches for a target value in a 2D matrix using binary search. + * + * @param matrix the 2D matrix + * @param target the value to search + * @return true if found, false otherwise + */ + public static boolean searchMatrix(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) / 2; + + int midValue = matrix[mid / cols][mid % cols]; + + if (midValue == target) { + return true; + } else if (midValue < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + + return false; + } +} From 3a364c6e960013e4c74261d0996760526c10f7d9 Mon Sep 17 00:00:00 2001 From: AHVSSATHVIK Date: Tue, 25 Nov 2025 07:21:50 +0000 Subject: [PATCH 2/4] Add Search2DMatrix algorithm --- .../thealgorithms/matrix/Search2DMatrix.java | 27 ++++--------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java index e7b642e93767..b4b9cfbf9ce8 100644 --- a/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java +++ b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java @@ -2,21 +2,10 @@ /** * Search a 2D Matrix - * Each row is sorted left to right, and the first element of each row - * is greater than the last element of the previous row. - * - * Time Complexity: O(log(m*n)) - * Space Complexity: O(1) + * Reference: https://leetcode.com/problems/search-a-2d-matrix/ */ public class Search2DMatrix { - /** - * Searches for a target value in a 2D matrix using binary search. - * - * @param matrix the 2D matrix - * @param target the value to search - * @return true if found, false otherwise - */ public static boolean searchMatrix(int[][] matrix, int target) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return false; @@ -24,24 +13,18 @@ public static boolean searchMatrix(int[][] matrix, int target) { int rows = matrix.length; int cols = matrix[0].length; - int left = 0; int right = rows * cols - 1; while (left <= right) { int mid = (left + right) / 2; - int midValue = matrix[mid / cols][mid % cols]; - if (midValue == target) { - return true; - } else if (midValue < target) { - left = mid + 1; - } else { - right = mid - 1; - } + if (midValue == target) return true; + else if (midValue < target) left = mid + 1; + else right = mid - 1; } - return false; } } + From 36ef64ac6f1315668b1609d43e5b5be2fe4de0dc Mon Sep 17 00:00:00 2001 From: AHVSSATHVIK Date: Tue, 25 Nov 2025 07:29:02 +0000 Subject: [PATCH 3/4] Fix checkstyle violations --- .../thealgorithms/matrix/Search2DMatrix.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java index b4b9cfbf9ce8..31e0de4e3ccd 100644 --- a/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java +++ b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java @@ -1,11 +1,23 @@ package com.thealgorithms.matrix; /** - * Search a 2D Matrix + * Search a 2D Matrix. * Reference: https://leetcode.com/problems/search-a-2d-matrix/ */ public class Search2DMatrix { + // Private constructor to satisfy Checkstyle utility class rule + private Search2DMatrix() { + throw new UnsupportedOperationException("Utility class"); + } + + /** + * Performs binary search on a 2D matrix. + * + * @param matrix the 2D matrix + * @param target the value to search + * @return true if found, false otherwise + */ public static boolean searchMatrix(int[][] matrix, int target) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return false; @@ -20,11 +32,15 @@ public static boolean searchMatrix(int[][] matrix, int target) { int mid = (left + right) / 2; int midValue = matrix[mid / cols][mid % cols]; - if (midValue == target) return true; - else if (midValue < target) left = mid + 1; - else right = mid - 1; + if (midValue == target) { + return true; + } else if (midValue < target) { + left = mid + 1; + } else { + right = mid - 1; + } } + return false; } } - From 3018106ee629d4f1d5a7a0d64150fdd63c2390ae Mon Sep 17 00:00:00 2001 From: AHVSSATHVIK Date: Tue, 25 Nov 2025 07:35:56 +0000 Subject: [PATCH 4/4] Make Search2DMatrix final (fix checkstyle) --- src/main/java/com/thealgorithms/matrix/Search2DMatrix.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java index 31e0de4e3ccd..bd7580eba002 100644 --- a/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java +++ b/src/main/java/com/thealgorithms/matrix/Search2DMatrix.java @@ -4,9 +4,9 @@ * Search a 2D Matrix. * Reference: https://leetcode.com/problems/search-a-2d-matrix/ */ -public class Search2DMatrix { +public final class Search2DMatrix { - // Private constructor to satisfy Checkstyle utility class rule + // Private constructor for utility class private Search2DMatrix() { throw new UnsupportedOperationException("Utility class"); }