Skip to content

Commit edf8e1e

Browse files
authored
Merge branch 'master' into Feature-ShuffleArray
2 parents 799d7dc + 857d921 commit edf8e1e

File tree

89 files changed

+4527
-1067
lines changed

Some content is hidden

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

89 files changed

+4527
-1067
lines changed

DIRECTORY.md

Lines changed: 27 additions & 1 deletion
Large diffs are not rendered by default.

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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>
@@ -125,7 +125,7 @@
125125
<dependency>
126126
<groupId>com.puppycrawl.tools</groupId>
127127
<artifactId>checkstyle</artifactId>
128-
<version>10.18.2</version>
128+
<version>10.19.0</version>
129129
</dependency>
130130
</dependencies>
131131
</plugin>
@@ -153,7 +153,7 @@
153153
<plugin>
154154
<groupId>org.apache.maven.plugins</groupId>
155155
<artifactId>maven-pmd-plugin</artifactId>
156-
<version>3.25.0</version>
156+
<version>3.26.0</version>
157157
<configuration>
158158
<printFailingErrors>true</printFailingErrors>
159159
<includeTests>true</includeTests>

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
}
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+
}
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+
}

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

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

33
/**
4-
* Converts any Octal Number to a Binary Number
4+
* This class provides a method to reverse the bits of a 32-bit integer.
5+
* Reversing the bits means that the least significant bit (LSB) becomes
6+
* the most significant bit (MSB) and vice versa.
7+
*
8+
* Example:
9+
* Input (binary): 00000010100101000001111010011100 (43261596)
10+
* Output (binary): 00111001011110000010100101000000 (964176192)
11+
*
12+
* Time Complexity: O(32) - A fixed number of 32 iterations
13+
* Space Complexity: O(1) - No extra space used
14+
*
15+
* Note:
16+
* - If the input is negative, Java handles it using two’s complement representation.
17+
* - This function works on 32-bit integers by default.
18+
*
519
* @author Bama Charan Chhandogi
620
*/
7-
821
public final class ReverseBits {
922
private ReverseBits() {
1023
}
1124

25+
/**
26+
* Reverses the bits of a 32-bit integer.
27+
*
28+
* @param n the integer whose bits are to be reversed
29+
* @return the integer obtained by reversing the bits of the input
30+
*/
1231
public static int reverseBits(int n) {
1332
int result = 0;
1433
int bitCount = 32;

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Swap every pair of adjacent bits of a given number.
5-
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
4+
* A utility class to swap every pair of adjacent bits in a given integer.
5+
* This operation shifts the even-positioned bits to odd positions and vice versa.
6+
*
7+
* Example:
8+
* - Input: 2 (binary: `10`) → Output: 1 (binary: `01`)
9+
* - Input: 43 (binary: `101011`) → Output: 23 (binary: `010111`)
10+
*
11+
* **Explanation of the Algorithm:**
12+
* 1. Mask even-positioned bits: Using `0xAAAAAAAA` (binary: `101010...`),
13+
* which selects bits in even positions.
14+
* 2. Mask odd-positioned bits: Using `0x55555555` (binary: `010101...`),
15+
* which selects bits in odd positions.
16+
* 3. Shift bits:
17+
* - Right-shift even-positioned bits by 1 to move them to odd positions.
18+
* - Left-shift odd-positioned bits by 1 to move them to even positions.
19+
* 4. Combine both shifted results using bitwise OR (`|`) to produce the final result.
20+
*
21+
* Use Case: This algorithm can be useful in applications involving low-level bit manipulation,
22+
* such as encoding, data compression, or cryptographic transformations.
23+
*
24+
* Time Complexity: O(1) (constant time, since operations are bitwise).
25+
*
26+
* Author: Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
627
*/
7-
828
public final class SwapAdjacentBits {
929
private SwapAdjacentBits() {
1030
}
1131

32+
/**
33+
* Swaps every pair of adjacent bits of a given integer.
34+
* Steps:
35+
* 1. Mask the even-positioned bits.
36+
* 2. Mask the odd-positioned bits.
37+
* 3. Shift the even bits to the right and the odd bits to the left.
38+
* 4. Combine the shifted bits.
39+
*
40+
* @param num the integer whose bits are to be swapped
41+
* @return the integer after swapping every pair of adjacent bits
42+
*/
1243
public static int swapAdjacentBits(int num) {
1344
// mask the even bits (0xAAAAAAAA => 10101010...)
1445
int evenBits = num & 0xAAAAAAAA;

0 commit comments

Comments
 (0)