From ca45522394deb899bc580ec759353371053edc42 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Mon, 2 Nov 2026 11:27:35 +0530 Subject: [PATCH] fix(searches): fix out-of-bounds in single-row and single-column in BinarySearch2dArray --- .../searches/BinarySearch2dArray.java | 9 +++- .../searches/BinarySearch2dArrayTest.java | 53 ++++++++++++++++++- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/BinarySearch2dArray.java b/src/main/java/com/thealgorithms/searches/BinarySearch2dArray.java index aa938447b864..317db0dead05 100644 --- a/src/main/java/com/thealgorithms/searches/BinarySearch2dArray.java +++ b/src/main/java/com/thealgorithms/searches/BinarySearch2dArray.java @@ -28,13 +28,13 @@ private BinarySearch2dArray() { * @return An array containing the row and column indices of the target, or [-1, * -1] if the target is not found. */ - static int[] binarySearch(int[][] arr, int target) { + public static int[] binarySearch(int[][] arr, int target) { int rowCount = arr.length; int colCount = arr[0].length; // Edge case: If there's only one row, search that row directly. if (rowCount == 1) { - return binarySearch(arr, target, 0, 0, colCount); + return binarySearch(arr, target, 0, 0, colCount - 1); } // Set initial boundaries for binary search on rows. @@ -70,6 +70,11 @@ else if (arr[midRow][midCol] < target) { return new int[] {endRow, midCol}; } + // Edge case: If there is only one column, target cannot be outside midCol. + if (colCount == 1) { + return new int[] {-1, -1}; + } + // If target is smaller than the element in the left of startRow, perform a // binary search on the left of startRow. if (target <= arr[startRow][midCol - 1]) { diff --git a/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java b/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java index dec2c86de9c7..236e1be5cc23 100644 --- a/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java +++ b/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java @@ -1,5 +1,6 @@ package com.thealgorithms.searches; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -84,7 +85,7 @@ public void binarySearch2dArrayTestLowerSide() { // valid test case public void binarySearch2dArrayTestNotFound() { int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; - int target = 101; + int target = 100; int[] ans = BinarySearch2dArray.binarySearch(arr, target); System.out.println(Arrays.toString(ans)); @@ -93,7 +94,7 @@ public void binarySearch2dArrayTestNotFound() { } /** - * Test if the method works with input arrays consisting only of one row. + * Test if the method works with an array that only has one row. */ @Test public void binarySearch2dArrayTestOneRow() { @@ -154,4 +155,52 @@ public void binarySearch2dArrayTestEmptyArray() { // Assert that an empty array is not valid input for the method. assertThrows(ArrayIndexOutOfBoundsException.class, () -> BinarySearch2dArray.binarySearch(arr, target)); } + + @Test + public void binarySearch2dArrayTestSingleRowTargetNotFoundGreater() { + int[][] arr = {{1, 2, 3, 4}}; + int target = 10; + int[] ans = BinarySearch2dArray.binarySearch(arr, target); + assertArrayEquals(new int[] {-1, -1}, ans); + } + + @Test + public void binarySearch2dArrayTestSingleRowTargetNotFoundSmaller() { + int[][] arr = {{1, 2, 3, 4}}; + int target = 0; + int[] ans = BinarySearch2dArray.binarySearch(arr, target); + assertArrayEquals(new int[] {-1, -1}, ans); + } + + @Test + public void binarySearch2dArrayTestSingleColumnPresent() { + int[][] arr = {{1}, {3}, {5}}; + int target = 3; + int[] ans = BinarySearch2dArray.binarySearch(arr, target); + assertArrayEquals(new int[] {1, 0}, ans); + } + + @Test + public void binarySearch2dArrayTestSingleColumnNotFound() { + int[][] arr = {{1}, {3}, {5}}; + int target = 4; + int[] ans = BinarySearch2dArray.binarySearch(arr, target); + assertArrayEquals(new int[] {-1, -1}, ans); + } + + @Test + public void binarySearch2dArrayTestSingleElementPresent() { + int[][] arr = {{42}}; + int target = 42; + int[] ans = BinarySearch2dArray.binarySearch(arr, target); + assertArrayEquals(new int[] {0, 0}, ans); + } + + @Test + public void binarySearch2dArrayTestSingleElementNotFound() { + int[][] arr = {{42}}; + int target = 10; + int[] ans = BinarySearch2dArray.binarySearch(arr, target); + assertArrayEquals(new int[] {-1, -1}, ans); + } }