Skip to content

Commit ca45522

Browse files
committed
fix(searches): fix out-of-bounds in single-row and single-column in BinarySearch2dArray
1 parent 8b49bc5 commit ca45522

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎src/main/java/com/thealgorithms/searches/BinarySearch2dArray.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ private BinarySearch2dArray() {
2828
* @return An array containing the row and column indices of the target, or [-1,
2929
* -1] if the target is not found.
3030
*/
31-
static int[] binarySearch(int[][] arr, int target) {
31+
public static int[] binarySearch(int[][] arr, int target) {
3232
int rowCount = arr.length;
3333
int colCount = arr[0].length;
3434

3535
// Edge case: If there's only one row, search that row directly.
3636
if (rowCount == 1) {
37-
return binarySearch(arr, target, 0, 0, colCount);
37+
return binarySearch(arr, target, 0, 0, colCount - 1);
3838
}
3939

4040
// Set initial boundaries for binary search on rows.
@@ -70,6 +70,11 @@ else if (arr[midRow][midCol] < target) {
7070
return new int[] {endRow, midCol};
7171
}
7272

73+
// Edge case: If there is only one column, target cannot be outside midCol.
74+
if (colCount == 1) {
75+
return new int[] {-1, -1};
76+
}
77+
7378
// If target is smaller than the element in the left of startRow, perform a
7479
// binary search on the left of startRow.
7580
if (target <= arr[startRow][midCol - 1]) {

‎src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.thealgorithms.searches;
22

3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45
import static org.junit.jupiter.api.Assertions.assertThrows;
56

@@ -84,7 +85,7 @@ public void binarySearch2dArrayTestLowerSide() {
8485
// valid test case
8586
public void binarySearch2dArrayTestNotFound() {
8687
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
87-
int target = 101;
88+
int target = 100;
8889

8990
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
9091
System.out.println(Arrays.toString(ans));
@@ -93,7 +94,7 @@ public void binarySearch2dArrayTestNotFound() {
9394
}
9495

9596
/**
96-
* Test if the method works with input arrays consisting only of one row.
97+
* Test if the method works with an array that only has one row.
9798
*/
9899
@Test
99100
public void binarySearch2dArrayTestOneRow() {
@@ -154,4 +155,52 @@ public void binarySearch2dArrayTestEmptyArray() {
154155
// Assert that an empty array is not valid input for the method.
155156
assertThrows(ArrayIndexOutOfBoundsException.class, () -> BinarySearch2dArray.binarySearch(arr, target));
156157
}
158+
159+
@Test
160+
public void binarySearch2dArrayTestSingleRowTargetNotFoundGreater() {
161+
int[][] arr = {{1, 2, 3, 4}};
162+
int target = 10;
163+
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
164+
assertArrayEquals(new int[] {-1, -1}, ans);
165+
}
166+
167+
@Test
168+
public void binarySearch2dArrayTestSingleRowTargetNotFoundSmaller() {
169+
int[][] arr = {{1, 2, 3, 4}};
170+
int target = 0;
171+
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
172+
assertArrayEquals(new int[] {-1, -1}, ans);
173+
}
174+
175+
@Test
176+
public void binarySearch2dArrayTestSingleColumnPresent() {
177+
int[][] arr = {{1}, {3}, {5}};
178+
int target = 3;
179+
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
180+
assertArrayEquals(new int[] {1, 0}, ans);
181+
}
182+
183+
@Test
184+
public void binarySearch2dArrayTestSingleColumnNotFound() {
185+
int[][] arr = {{1}, {3}, {5}};
186+
int target = 4;
187+
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
188+
assertArrayEquals(new int[] {-1, -1}, ans);
189+
}
190+
191+
@Test
192+
public void binarySearch2dArrayTestSingleElementPresent() {
193+
int[][] arr = {{42}};
194+
int target = 42;
195+
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
196+
assertArrayEquals(new int[] {0, 0}, ans);
197+
}
198+
199+
@Test
200+
public void binarySearch2dArrayTestSingleElementNotFound() {
201+
int[][] arr = {{42}};
202+
int target = 10;
203+
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
204+
assertArrayEquals(new int[] {-1, -1}, ans);
205+
}
157206
}

0 commit comments

Comments
 (0)