Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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));
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
}
Loading