Skip to content

Commit 392cf58

Browse files
authored
Merge branch 'master' into fix/catalan-number-zero-input
2 parents 3c3d25a + 0a76bf8 commit 392cf58

4 files changed

Lines changed: 73 additions & 4 deletions

File tree

‎src/main/java/com/thealgorithms/maths/BinomialCoefficient.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ private BinomialCoefficient() {
2121
* @param numberOfObjects Number of objects to be chosen from total_objects
2222
* @return number of ways in which no_of_objects objects can be chosen from total_objects
2323
* objects
24+
* @throws IllegalArgumentException if totalObjects or numberOfObjects is negative
2425
*/
2526

2627
public static int binomialCoefficient(int totalObjects, int numberOfObjects) {
28+
if (totalObjects < 0 || numberOfObjects < 0) {
29+
throw new IllegalArgumentException("Arguments must be non-negative.");
30+
}
31+
2732
// Base Case
2833
if (numberOfObjects > totalObjects) {
2934
return 0;

‎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/maths/BinomialCoefficientTest.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.maths;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import org.junit.jupiter.api.Test;
67

@@ -12,5 +13,14 @@ void testBinomialCoefficient() {
1213
assertEquals(792, BinomialCoefficient.binomialCoefficient(12, 5));
1314
assertEquals(84, BinomialCoefficient.binomialCoefficient(9, 3));
1415
assertEquals(1, BinomialCoefficient.binomialCoefficient(17, 17));
16+
assertEquals(0, BinomialCoefficient.binomialCoefficient(5, 6));
17+
assertEquals(1, BinomialCoefficient.binomialCoefficient(0, 0));
18+
}
19+
20+
@Test
21+
void testNegativeInputs() {
22+
assertThrows(IllegalArgumentException.class, () -> BinomialCoefficient.binomialCoefficient(5, -1));
23+
assertThrows(IllegalArgumentException.class, () -> BinomialCoefficient.binomialCoefficient(-1, 5));
24+
assertThrows(IllegalArgumentException.class, () -> BinomialCoefficient.binomialCoefficient(-5, -2));
1525
}
1626
}

‎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)