Skip to content

Commit eab40d6

Browse files
authored
Merge branch 'master' into dependabot/maven/org.apache.maven.plugins-maven-checkstyle-plugin-3.6.0
2 parents 7ba94f5 + 985c1f9 commit eab40d6

File tree

149 files changed

+7563
-1451
lines changed

Some content is hidden

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

149 files changed

+7563
-1451
lines changed

DIRECTORY.md

+67-2
Large diffs are not rendered by default.

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

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

+16-2
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;
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+
}
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/NonRepeatingNumberFinder.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Find Non Repeating Number
4+
* A utility class to find the non-repeating number in an array where every other number repeats.
5+
* This class contains a method to identify the single unique number using bit manipulation.
6+
*
7+
* The solution leverages the properties of the XOR operation, which states that:
8+
* - x ^ x = 0 for any integer x (a number XORed with itself is zero)
9+
* - x ^ 0 = x for any integer x (a number XORed with zero is the number itself)
10+
*
11+
* Using these properties, we can find the non-repeating number in linear time with constant space.
12+
*
13+
* Example:
14+
* Given the input array [2, 3, 5, 2, 3], the output will be 5 since it does not repeat.
15+
*
516
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
617
*/
7-
818
public final class NonRepeatingNumberFinder {
919
private NonRepeatingNumberFinder() {
1020
}
1121

22+
/**
23+
* Finds the non-repeating number in the given array.
24+
*
25+
* @param arr an array of integers where every number except one appears twice
26+
* @return the integer that appears only once in the array or 0 if the array is empty
27+
*/
1228
public static int findNonRepeatingNumber(int[] arr) {
1329
int result = 0;
1430
for (int num : arr) {
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Find the Number Appearing Odd Times in an array
4+
* This class provides a method to find the element that appears an
5+
* odd number of times in an array. All other elements in the array
6+
* must appear an even number of times for the logic to work.
7+
*
8+
* The solution uses the XOR operation, which has the following properties:
9+
* - a ^ a = 0 (XOR-ing the same numbers cancels them out)
10+
* - a ^ 0 = a
11+
* - XOR is commutative and associative.
12+
*
13+
* Time Complexity: O(n), where n is the size of the array.
14+
* Space Complexity: O(1), as no extra space is used.
15+
*
16+
* Usage Example:
17+
* int result = NumberAppearingOddTimes.findOddOccurrence(new int[]{1, 2, 1, 2, 3});
18+
* // result will be 3
19+
*
520
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
621
*/
722

823
public final class NumberAppearingOddTimes {
924
private NumberAppearingOddTimes() {
1025
}
26+
27+
/**
28+
* Finds the element in the array that appears an odd number of times.
29+
*
30+
* @param arr the input array containing integers, where all elements
31+
* except one appear an even number of times.
32+
* @return the integer that appears an odd number of times.
33+
*/
1134
public static int findOddOccurrence(int[] arr) {
1235
int result = 0;
13-
14-
// XOR all elements in the array
1536
for (int num : arr) {
1637
result ^= num;
1738
}
18-
1939
return result;
2040
}
2141
}

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Numbers Different Signs
4+
* This class provides a method to determine whether two integers have
5+
* different signs. It utilizes the XOR operation on the two numbers:
6+
*
7+
* - If two numbers have different signs, their most significant bits
8+
* (sign bits) will differ, resulting in a negative XOR result.
9+
* - If two numbers have the same sign, the XOR result will be non-negative.
10+
*
11+
* Time Complexity: O(1) - Constant time operation.
12+
* Space Complexity: O(1) - No extra space used.
13+
*
514
* @author Bama Charan Chhandogi
615
*/
7-
816
public final class NumbersDifferentSigns {
917
private NumbersDifferentSigns() {
1018
}
1119

20+
/**
21+
* Determines if two integers have different signs using bitwise XOR.
22+
*
23+
* @param num1 the first integer
24+
* @param num2 the second integer
25+
* @return true if the two numbers have different signs, false otherwise
26+
*/
1227
public static boolean differentSigns(int num1, int num2) {
1328
return (num1 ^ num2) < 0;
1429
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides a method to detect if two integers
5+
* differ by exactly one bit flip.
6+
*
7+
* Example:
8+
* 1 (0001) and 2 (0010) differ by exactly one bit flip.
9+
* 7 (0111) and 3 (0011) differ by exactly one bit flip.
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class OneBitDifference {
14+
private OneBitDifference() {
15+
}
16+
17+
/**
18+
* Checks if two integers differ by exactly one bit.
19+
*
20+
* @param x the first integer
21+
* @param y the second integer
22+
* @return true if x and y differ by exactly one bit, false otherwise
23+
*/
24+
public static boolean differByOneBit(int x, int y) {
25+
if (x == y) {
26+
return false;
27+
}
28+
29+
int xor = x ^ y;
30+
return (xor & (xor - 1)) == 0;
31+
}
32+
}

0 commit comments

Comments
 (0)