Skip to content

Commit f24ca6c

Browse files
committed
Merge remote-tracking branch 'origin/speculative_new_algo' into speculative_new_algo
2 parents 33c7c02 + 2ecbfba commit f24ca6c

File tree

313 files changed

+14184
-2681
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

313 files changed

+14184
-2681
lines changed

DIRECTORY.md

Lines changed: 138 additions & 2 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>5.11.2</version>
23+
<version>5.11.3</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>org.junit.jupiter</groupId>
3333
<artifactId>junit-jupiter</artifactId>
34-
<version>5.11.2</version>
34+
<version>5.11.3</version>
3535
<scope>test</scope>
3636
</dependency>
3737
<dependency>
@@ -43,15 +43,15 @@
4343
<dependency>
4444
<groupId>org.mockito</groupId>
4545
<artifactId>mockito-core</artifactId>
46-
<version>5.14.1</version>
46+
<version>5.14.2</version>
4747
<scope>test</scope>
4848
</dependency>
4949

5050

5151
<dependency>
5252
<groupId>org.junit.jupiter</groupId>
5353
<artifactId>junit-jupiter-api</artifactId>
54-
<version>5.11.2</version>
54+
<version>5.11.3</version>
5555
<scope>test</scope>
5656
</dependency>
5757
<dependency>
@@ -114,7 +114,7 @@
114114
<plugin>
115115
<groupId>org.apache.maven.plugins</groupId>
116116
<artifactId>maven-checkstyle-plugin</artifactId>
117-
<version>3.5.0</version>
117+
<version>3.6.0</version>
118118
<configuration>
119119
<configLocation>checkstyle.xml</configLocation>
120120
<consoleOutput>true</consoleOutput>
@@ -132,7 +132,7 @@
132132
<plugin>
133133
<groupId>com.github.spotbugs</groupId>
134134
<artifactId>spotbugs-maven-plugin</artifactId>
135-
<version>4.8.6.4</version>
135+
<version>4.8.6.5</version>
136136
<configuration>
137137
<excludeFilterFile>spotbugs-exclude.xml</excludeFilterFile>
138138
<includeTests>true</includeTests>

spotbugs-exclude.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@
8787
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" />
8888
</Match>
8989
<!-- fb-contrib -->
90-
<Match>
91-
<Bug pattern="OCP_OVERLY_CONCRETE_PARAMETER" />
92-
</Match>
9390
<Match>
9491
<Bug pattern="LSC_LITERAL_STRING_COMPARISON" />
9592
</Match>

src/main/java/com/thealgorithms/backtracking/ArrayCombination.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,44 @@
44
import java.util.List;
55

66
/**
7-
* Finds all combinations of 0...n-1 of length k
7+
* This class provides methods to find all combinations of integers from 0 to n-1
8+
* of a specified length k using backtracking.
89
*/
910
public final class ArrayCombination {
1011
private ArrayCombination() {
1112
}
1213

1314
/**
14-
* Finds all combinations of length k of 0..n-1 using backtracking.
15+
* Generates all possible combinations of length k from the integers 0 to n-1.
1516
*
16-
* @param n Number of the elements.
17-
* @param k Length of the combination.
18-
* @return A list of all combinations of length k.
17+
* @param n The total number of elements (0 to n-1).
18+
* @param k The desired length of each combination.
19+
* @return A list containing all combinations of length k.
20+
* @throws IllegalArgumentException if n or k are negative, or if k is greater than n.
1921
*/
2022
public static List<List<Integer>> combination(int n, int k) {
2123
if (n < 0 || k < 0 || k > n) {
22-
throw new IllegalArgumentException("Wrong input.");
24+
throw new IllegalArgumentException("Invalid input: n must be non-negative, k must be non-negative and less than or equal to n.");
2325
}
2426

2527
List<List<Integer>> combinations = new ArrayList<>();
2628
combine(combinations, new ArrayList<>(), 0, n, k);
2729
return combinations;
2830
}
2931

32+
/**
33+
* A helper method that uses backtracking to find combinations.
34+
*
35+
* @param combinations The list to store all valid combinations found.
36+
* @param current The current combination being built.
37+
* @param start The starting index for the current recursion.
38+
* @param n The total number of elements (0 to n-1).
39+
* @param k The desired length of each combination.
40+
*/
3041
private static void combine(List<List<Integer>> combinations, List<Integer> current, int start, int n, int k) {
31-
if (current.size() == k) { // Base case: combination found
32-
combinations.add(new ArrayList<>(current)); // Copy to avoid modification
42+
// Base case: combination found
43+
if (current.size() == k) {
44+
combinations.add(new ArrayList<>(current));
3345
return;
3446
}
3547

src/main/java/com/thealgorithms/backtracking/Combination.java

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

33
import java.util.Arrays;
4+
import java.util.Collections;
45
import java.util.LinkedList;
56
import java.util.List;
67
import java.util.TreeSet;
@@ -13,8 +14,6 @@ public final class Combination {
1314
private Combination() {
1415
}
1516

16-
private static int length;
17-
1817
/**
1918
* Find all combinations of given array using backtracking
2019
* @param arr the array.
@@ -23,39 +22,45 @@ private Combination() {
2322
* @return a list of all combinations of length n. If n == 0, return null.
2423
*/
2524
public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
25+
if (n < 0) {
26+
throw new IllegalArgumentException("The combination length cannot be negative.");
27+
}
28+
2629
if (n == 0) {
27-
return null;
30+
return Collections.emptyList();
2831
}
29-
length = n;
3032
T[] array = arr.clone();
3133
Arrays.sort(array);
34+
3235
List<TreeSet<T>> result = new LinkedList<>();
33-
backtracking(array, 0, new TreeSet<T>(), result);
36+
backtracking(array, n, 0, new TreeSet<T>(), result);
3437
return result;
3538
}
3639

3740
/**
3841
* Backtrack all possible combinations of a given array
3942
* @param arr the array.
43+
* @param n length of the combination
4044
* @param index the starting index.
4145
* @param currSet set that tracks current combination
4246
* @param result the list contains all combination.
4347
* @param <T> the type of elements in the array.
4448
*/
45-
private static <T> void backtracking(T[] arr, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
46-
if (index + length - currSet.size() > arr.length) {
49+
private static <T> void backtracking(T[] arr, int n, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
50+
if (index + n - currSet.size() > arr.length) {
4751
return;
4852
}
49-
if (length - 1 == currSet.size()) {
53+
if (currSet.size() == n - 1) {
5054
for (int i = index; i < arr.length; i++) {
5155
currSet.add(arr[i]);
52-
result.add((TreeSet<T>) currSet.clone());
56+
result.add(new TreeSet<>(currSet));
5357
currSet.remove(arr[i]);
5458
}
59+
return;
5560
}
5661
for (int i = index; i < arr.length; i++) {
5762
currSet.add(arr[i]);
58-
backtracking(arr, i + 1, currSet, result);
63+
backtracking(arr, n, i + 1, currSet, result);
5964
currSet.remove(arr[i]);
6065
}
6166
}

src/main/java/com/thealgorithms/backtracking/CrosswordSolver.java

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

33
import java.util.ArrayList;
4+
import java.util.Collection;
45
import java.util.List;
56

67
/**
@@ -95,7 +96,7 @@ public static void removeWord(char[][] puzzle, String word, int row, int col, bo
9596
* @param words The list of words to be placed.
9697
* @return true if the crossword is solved, false otherwise.
9798
*/
98-
public static boolean solveCrossword(char[][] puzzle, List<String> words) {
99+
public static boolean solveCrossword(char[][] puzzle, Collection<String> words) {
99100
// Create a mutable copy of the words list
100101
List<String> remainingWords = new ArrayList<>(words);
101102

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to find the first differing bit
5+
* between two integers.
6+
*
7+
* Example:
8+
* x = 10 (1010 in binary)
9+
* y = 12 (1100 in binary)
10+
* The first differing bit is at index 1 (0-based)
11+
* So, the output will be 1
12+
*
13+
* @author Hardvan
14+
*/
15+
public final class FirstDifferentBit {
16+
private FirstDifferentBit() {
17+
}
18+
19+
/**
20+
* Identifies the index of the first differing bit between two integers.
21+
* Steps:
22+
* 1. XOR the two integers to get the differing bits
23+
* 2. Find the index of the first set bit in XOR result
24+
*
25+
* @param x the first integer
26+
* @param y the second integer
27+
* @return the index of the first differing bit (0-based)
28+
*/
29+
public static int firstDifferentBit(int x, int y) {
30+
int diff = x ^ y;
31+
return Integer.numberOfTrailingZeros(diff);
32+
}
33+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* This class provides a method to generate all subsets (power set)
8+
* of a given set using bit manipulation.
9+
*
10+
* @author Hardvan
11+
*/
12+
public final class GenerateSubsets {
13+
private GenerateSubsets() {
14+
}
15+
16+
/**
17+
* Generates all subsets of a given set using bit manipulation.
18+
* Steps:
19+
* 1. Iterate over all numbers from 0 to 2^n - 1.
20+
* 2. For each number, iterate over all bits from 0 to n - 1.
21+
* 3. If the i-th bit of the number is set, add the i-th element of the set to the current subset.
22+
* 4. Add the current subset to the list of subsets.
23+
* 5. Return the list of subsets.
24+
*
25+
* @param set the input set of integers
26+
* @return a list of all subsets represented as lists of integers
27+
*/
28+
public static List<List<Integer>> generateSubsets(int[] set) {
29+
int n = set.length;
30+
List<List<Integer>> subsets = new ArrayList<>();
31+
32+
for (int mask = 0; mask < (1 << n); mask++) {
33+
List<Integer> subset = new ArrayList<>();
34+
for (int i = 0; i < n; i++) {
35+
if ((mask & (1 << i)) != 0) {
36+
subset.add(set[i]);
37+
}
38+
}
39+
subsets.add(subset);
40+
}
41+
42+
return subsets;
43+
}
44+
}

src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Is number power of 2
4+
* Utility class for checking if a number is a power of two.
5+
* A power of two is a number that can be expressed as 2^n where n is a non-negative integer.
6+
* This class provides a method to determine if a given integer is a power of two using bit manipulation.
7+
*
58
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
69
*/
7-
810
public final class IsPowerTwo {
911
private IsPowerTwo() {
1012
}
13+
14+
/**
15+
* Checks if the given integer is a power of two.
16+
*
17+
* A number is considered a power of two if it is greater than zero and
18+
* has exactly one '1' bit in its binary representation. This method
19+
* uses the property that for any power of two (n), the expression
20+
* (n & (n - 1)) will be zero.
21+
*
22+
* @param number the integer to check
23+
* @return true if the number is a power of two, false otherwise
24+
*/
1125
public static boolean isPowerTwo(int number) {
1226
if (number <= 0) {
1327
return false;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to compute the remainder
5+
* of a number when divided by a power of two (2^n)
6+
* without using division or modulo operations.
7+
*
8+
* @author Hardvan
9+
*/
10+
public final class ModuloPowerOfTwo {
11+
private ModuloPowerOfTwo() {
12+
}
13+
14+
/**
15+
* Computes the remainder of a given integer when divided by 2^n.
16+
*
17+
* @param x the input number
18+
* @param n the exponent (power of two)
19+
* @return the remainder of x divided by 2^n
20+
*/
21+
public static int moduloPowerOfTwo(int x, int n) {
22+
if (n <= 0) {
23+
throw new IllegalArgumentException("The exponent must be positive");
24+
}
25+
26+
return x & ((1 << n) - 1);
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to find the next higher number
5+
* with the same number of set bits as the given number.
6+
*
7+
* @author Hardvan
8+
*/
9+
public final class NextHigherSameBitCount {
10+
private NextHigherSameBitCount() {
11+
}
12+
13+
/**
14+
* Finds the next higher integer with the same number of set bits.
15+
* Steps:
16+
* 1. Find {@code c}, the rightmost set bit of {@code n}.
17+
* 2. Find {@code r}, the rightmost set bit of {@code n + c}.
18+
* 3. Swap the bits of {@code r} and {@code n} to the right of {@code c}.
19+
* 4. Shift the bits of {@code r} and {@code n} to the right of {@code c} to the rightmost.
20+
* 5. Combine the results of steps 3 and 4.
21+
*
22+
* @param n the input number
23+
* @return the next higher integer with the same set bit count
24+
*/
25+
public static int nextHigherSameBitCount(int n) {
26+
int c = n & -n;
27+
int r = n + c;
28+
return (((r ^ n) >> 2) / c) | r;
29+
}
30+
}

0 commit comments

Comments
 (0)