Skip to content

Commit cebb02d

Browse files
Merge branch 'master' into master
2 parents 426e0f3 + 0e8291e commit cebb02d

File tree

15 files changed

+487
-13
lines changed

15 files changed

+487
-13
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
<plugin>
128128
<groupId>com.mebigfatguy.fb-contrib</groupId>
129129
<artifactId>fb-contrib</artifactId>
130-
<version>7.7.3</version>
130+
<version>7.7.4</version>
131131
</plugin>
132132
<plugin>
133133
<groupId>com.h3xstream.findsecbugs</groupId>

spotbugs-exclude.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,21 @@
207207
<Match>
208208
<Bug pattern="FII_USE_ARRAYS_STREAM" />
209209
</Match>
210+
<Match>
211+
<Bug pattern="UTAO_JUNIT_ASSERTION_ODDITIES_USE_ASSERT_EQUALS" />
212+
</Match>
213+
<Match>
214+
<Bug pattern="UTAO_JUNIT_ASSERTION_ODDITIES_USE_ASSERT_NOT_EQUALS" />
215+
</Match>
216+
<Match>
217+
<Bug pattern="UTAO_JUNIT_ASSERTION_ODDITIES_ACTUAL_CONSTANT" />
218+
</Match>
219+
<Match>
220+
<Bug pattern="UTAO_JUNIT_ASSERTION_ODDITIES_INEXACT_DOUBLE" />
221+
</Match>
222+
<Match>
223+
<Bug pattern="UTAO_JUNIT_ASSERTION_ODDITIES_IMPOSSIBLE_NULL" />
224+
</Match>
210225
<!-- find-sec-bugs -->
211226
<Match>
212227
<Bug pattern="PREDICTABLE_RANDOM" />
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* The Bell numbers count the number of partitions of a set.
5+
* The n-th Bell number is the number of ways a set of n elements can be partitioned
6+
* into nonempty subsets.
7+
*
8+
* <p>
9+
* This implementation uses the Bell Triangle (Aitken's array) method.
10+
* Time Complexity: O(n^2)
11+
* Space Complexity: O(n^2)
12+
* </p>
13+
*
14+
* @author Chahat Sandhu, <a href="https://github.com/singhc7">singhc7</a>
15+
* @see <a href="https://en.wikipedia.org/wiki/Bell_number">Bell Number (Wikipedia)</a>
16+
*/
17+
public final class BellNumbers {
18+
19+
private BellNumbers() {
20+
}
21+
22+
/**
23+
* Calculates the n-th Bell number using the Bell Triangle.
24+
*
25+
* @param n the index of the Bell number (must be non-negative)
26+
* @return the n-th Bell number
27+
* @throws IllegalArgumentException if n is negative or n > 25
28+
*/
29+
public static long compute(int n) {
30+
if (n < 0) {
31+
throw new IllegalArgumentException("n must be non-negative");
32+
}
33+
if (n == 0) {
34+
return 1;
35+
}
36+
if (n > 25) {
37+
throw new IllegalArgumentException("n must be <= 25. For larger n, use BigInteger implementation.");
38+
}
39+
40+
// We use a 2D array to visualize the Bell Triangle
41+
long[][] bellTriangle = new long[n + 1][n + 1];
42+
43+
// Base case: The triangle starts with 1
44+
bellTriangle[0][0] = 1;
45+
46+
for (int i = 1; i <= n; i++) {
47+
// Rule 1: The first number in a new row is the LAST number of the previous row
48+
bellTriangle[i][0] = bellTriangle[i - 1][i - 1];
49+
50+
// Rule 2: Fill the rest of the row by adding the previous neighbor and the upper-left neighbor
51+
for (int j = 1; j <= i; j++) {
52+
bellTriangle[i][j] = bellTriangle[i][j - 1] + bellTriangle[i - 1][j - 1];
53+
}
54+
}
55+
56+
// The Bell number B_n is the first number in the n-th row
57+
return bellTriangle[n][0];
58+
}
59+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* Distance Between Two Points in 2D Space.
5+
*
6+
* <p>This class provides a method to calculate the Euclidean distance between two points in a
7+
* two-dimensional plane.
8+
*
9+
* <p>Formula: d = sqrt((x2 - x1)^2 + (y2 - y1)^2)
10+
*
11+
* <p>Reference: https://en.wikipedia.org/wiki/Euclidean_distance
12+
*/
13+
public final class DistanceBetweenTwoPoints {
14+
15+
private DistanceBetweenTwoPoints() {
16+
// Utility class; prevent instantiation
17+
}
18+
19+
/**
20+
* Calculate the Euclidean distance between two points.
21+
*
22+
* @param x1 x-coordinate of the first point
23+
* @param y1 y-coordinate of the first point
24+
* @param x2 x-coordinate of the second point
25+
* @param y2 y-coordinate of the second point
26+
* @return Euclidean distance between the two points
27+
*/
28+
public static double calculate(final double x1, final double y1, final double x2, final double y2) {
29+
final double deltaX = x2 - x1;
30+
final double deltaY = y2 - y1;
31+
return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
32+
}
33+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.thealgorithms.prefixsum;
2+
3+
/**
4+
* A class that implements the Prefix Sum algorithm.
5+
*
6+
* <p>Prefix Sum is a technique used to preprocess an array such that
7+
* range sum queries can be answered in O(1) time.
8+
* The preprocessing step takes O(N) time.
9+
*
10+
* <p>This implementation uses a long array for the prefix sums to prevent
11+
* integer overflow when the sum of elements exceeds Integer.MAX_VALUE.
12+
*
13+
* @see <a href="https://en.wikipedia.org/wiki/Prefix_sum">Prefix Sum (Wikipedia)</a>
14+
* @author Chahat Sandhu, <a href="https://github.com/singhc7">singhc7</a>
15+
*/
16+
public class PrefixSum {
17+
18+
private final long[] prefixSums;
19+
20+
/**
21+
* Constructor to preprocess the input array.
22+
*
23+
* @param array The input integer array.
24+
* @throws IllegalArgumentException if the array is null.
25+
*/
26+
public PrefixSum(int[] array) {
27+
if (array == null) {
28+
throw new IllegalArgumentException("Input array cannot be null");
29+
}
30+
this.prefixSums = new long[array.length + 1];
31+
this.prefixSums[0] = 0;
32+
33+
for (int i = 0; i < array.length; i++) {
34+
// Automatically promotes int to long during addition
35+
this.prefixSums[i + 1] = this.prefixSums[i] + array[i];
36+
}
37+
}
38+
39+
/**
40+
* Calculates the sum of elements in the range [left, right].
41+
* Indices are 0-based.
42+
*
43+
* @param left The starting index (inclusive).
44+
* @param right The ending index (inclusive).
45+
* @return The sum of elements from index left to right as a long.
46+
* @throws IndexOutOfBoundsException if indices are out of valid range.
47+
*/
48+
public long sumRange(int left, int right) {
49+
if (left < 0 || right >= prefixSums.length - 1 || left > right) {
50+
throw new IndexOutOfBoundsException("Invalid range indices");
51+
}
52+
return prefixSums[right + 1] - prefixSums[left];
53+
}
54+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.thealgorithms.prefixsum;
2+
3+
/**
4+
* A class that implements the 2D Prefix Sum algorithm.
5+
*
6+
* <p>2D Prefix Sum is a technique used to preprocess a 2D matrix such that
7+
* sub-matrix sum queries can be answered in O(1) time.
8+
* The preprocessing step takes O(N*M) time.
9+
*
10+
* <p>This implementation uses a long array for the prefix sums to prevent
11+
* integer overflow.
12+
*
13+
* @see <a href="https://en.wikipedia.org/wiki/Summed-area_table">Summed-area table (Wikipedia)</a>
14+
* @author Chahat Sandhu, <a href="https://github.com/singhc7">singhc7</a>
15+
*/
16+
public class PrefixSum2D {
17+
18+
private final long[][] prefixSums;
19+
20+
/**
21+
* Constructor to preprocess the input matrix.
22+
*
23+
* @param matrix The input integer matrix.
24+
* @throws IllegalArgumentException if the matrix is null or empty.
25+
*/
26+
public PrefixSum2D(int[][] matrix) {
27+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
28+
throw new IllegalArgumentException("Input matrix cannot be null or empty");
29+
}
30+
31+
int rows = matrix.length;
32+
int cols = matrix[0].length;
33+
this.prefixSums = new long[rows + 1][cols + 1];
34+
35+
for (int i = 0; i < rows; i++) {
36+
for (int j = 0; j < cols; j++) {
37+
// P[i+1][j+1] = current + above + left - diagonal_overlap
38+
this.prefixSums[i + 1][j + 1] = matrix[i][j] + this.prefixSums[i][j + 1] + this.prefixSums[i + 1][j] - this.prefixSums[i][j];
39+
}
40+
}
41+
}
42+
43+
/**
44+
* Calculates the sum of the sub-matrix defined by (row1, col1) to (row2, col2).
45+
* Indices are 0-based.
46+
*
47+
* @param row1 Top row index.
48+
* @param col1 Left column index.
49+
* @param row2 Bottom row index.
50+
* @param col2 Right column index.
51+
* @return The sum of the sub-matrix.
52+
* @throws IndexOutOfBoundsException if indices are invalid.
53+
*/
54+
public long sumRegion(int row1, int col1, int row2, int col2) {
55+
if (row1 < 0 || row2 >= prefixSums.length - 1 || row2 < row1) {
56+
throw new IndexOutOfBoundsException("Invalid row indices");
57+
}
58+
if (col1 < 0 || col2 >= prefixSums[0].length - 1 || col2 < col1) {
59+
throw new IndexOutOfBoundsException("Invalid column indices");
60+
}
61+
62+
return prefixSums[row2 + 1][col2 + 1] - prefixSums[row1][col2 + 1] - prefixSums[row2 + 1][col1] + prefixSums[row1][col1];
63+
}
64+
}

src/test/java/com/thealgorithms/ciphers/PermutationCipherTest.java

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

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

67
import org.junit.jupiter.api.Test;
@@ -121,8 +122,8 @@ void testNullString() {
121122
String decrypted = cipher.decrypt(encrypted, key);
122123

123124
// then
124-
assertEquals(null, encrypted);
125-
assertEquals(null, decrypted);
125+
assertNull(encrypted);
126+
assertNull(decrypted);
126127
}
127128

128129
@Test

src/test/java/com/thealgorithms/datastructures/heaps/HeapElementTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
56
import static org.junit.jupiter.api.Assertions.assertNull;
67

78
import org.junit.jupiter.api.Test;
@@ -39,7 +40,7 @@ void testEquals() {
3940

4041
assertEquals(element1, element2); // Same key and info
4142
assertNotEquals(element1, element3); // Different key
42-
assertNotEquals(null, element1); // Check for null
43+
assertNotNull(element1);
4344
assertNotEquals("String", element1); // Check for different type
4445
}
4546

src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
56

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

@@ -30,7 +31,7 @@ public void searchAndNotFound() {
3031
treap.insert(3);
3132
treap.insert(8);
3233
treap.insert(1);
33-
assertEquals(null, treap.search(4));
34+
assertNull(treap.search(4));
3435
}
3536

3637
@Test

src/test/java/com/thealgorithms/dynamicprogramming/LongestCommonSubsequenceTest.java

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

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

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

@@ -55,27 +56,24 @@ public void testLCSWithBothEmptyStrings() {
5556
public void testLCSWithNullFirstString() {
5657
String str1 = null;
5758
String str2 = "XYZ";
58-
String expected = null; // Should return null if first string is null
5959
String result = LongestCommonSubsequence.getLCS(str1, str2);
60-
assertEquals(expected, result);
60+
assertNull(result);
6161
}
6262

6363
@Test
6464
public void testLCSWithNullSecondString() {
6565
String str1 = "ABC";
6666
String str2 = null;
67-
String expected = null; // Should return null if second string is null
6867
String result = LongestCommonSubsequence.getLCS(str1, str2);
69-
assertEquals(expected, result);
68+
assertNull(result);
7069
}
7170

7271
@Test
7372
public void testLCSWithNullBothStrings() {
7473
String str1 = null;
7574
String str2 = null;
76-
String expected = null; // Should return null if both strings are null
7775
String result = LongestCommonSubsequence.getLCS(str1, str2);
78-
assertEquals(expected, result);
76+
assertNull(result);
7977
}
8078

8179
@Test

0 commit comments

Comments
 (0)