Skip to content

Commit 78cec9a

Browse files
authored
Merge branch 'master' into parity_new_algo
2 parents 4649c08 + e38611e commit 78cec9a

19 files changed

+564
-44
lines changed

DIRECTORY.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@
2323
* [WordPatternMatcher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordPatternMatcher.java)
2424
* [WordSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordSearch.java)
2525
* bitmanipulation
26+
* [BinaryPalindromeCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java)
2627
* [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java)
28+
* [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java)
29+
* [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java)
2730
* [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java)
31+
* [GrayCodeConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java)
32+
* [HammingDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HammingDistance.java)
33+
* [HigherLowerPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java)
2834
* [HighestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java)
2935
* [IndexOfRightMostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBit.java)
3036
* [IsEven](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsEven.java)
@@ -647,8 +653,14 @@
647653
* [WordPatternMatcherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordPatternMatcherTest.java)
648654
* [WordSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordSearchTest.java)
649655
* bitmanipulation
656+
* [BinaryPalindromeCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java)
650657
* [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java)
658+
* [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java)
659+
* [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java)
651660
* [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java)
661+
* [GrayCodeConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/GrayCodeConversionTest.java)
662+
* [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HammingDistanceTest.java)
663+
* [HigherLowerPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java)
652664
* [HighestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java)
653665
* [IndexOfRightMostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IndexOfRightMostSetBitTest.java)
654666
* [IsEvenTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsEvenTest.java)
@@ -826,6 +838,7 @@
826838
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
827839
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)
828840
* [MatrixChainMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplicationTest.java)
841+
* [MatrixChainRecursiveTopDownMemoisationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisationTest.java)
829842
* [MaximumSumOfNonAdjacentElementsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MaximumSumOfNonAdjacentElementsTest.java)
830843
* [MinimumPathSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumPathSumTest.java)
831844
* [MinimumSumPartitionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumSumPartitionTest.java)
@@ -1025,6 +1038,7 @@
10251038
* [KMPSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/KMPSearchTest.java)
10261039
* [LinearSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchTest.java)
10271040
* [LinearSearchThreadTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LinearSearchThreadTest.java)
1041+
* [LowerBoundTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/LowerBoundTest.java)
10281042
* [MonteCarloTreeSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/MonteCarloTreeSearchTest.java)
10291043
* [OrderAgnosticBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/OrderAgnosticBinarySearchTest.java)
10301044
* [PerfectBinarySearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/PerfectBinarySearchTest.java)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class contains a method to check if the binary representation of a number is a palindrome.
5+
* <p>
6+
* A binary palindrome is a number whose binary representation is the same when read from left to right and right to left.
7+
* For example, the number 9 has a binary representation of 1001, which is a palindrome.
8+
* The number 10 has a binary representation of 1010, which is not a palindrome.
9+
* </p>
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class BinaryPalindromeCheck {
14+
private BinaryPalindromeCheck() {
15+
}
16+
17+
/**
18+
* Checks if the binary representation of a number is a palindrome.
19+
*
20+
* @param x The number to check.
21+
* @return True if the binary representation is a palindrome, otherwise false.
22+
*/
23+
public static boolean isBinaryPalindrome(int x) {
24+
int reversed = reverseBits(x);
25+
return x == reversed;
26+
}
27+
28+
/**
29+
* Helper function to reverse all the bits of an integer.
30+
*
31+
* @param x The number to reverse the bits of.
32+
* @return The number with reversed bits.
33+
*/
34+
private static int reverseBits(int x) {
35+
int result = 0;
36+
while (x > 0) {
37+
result <<= 1;
38+
result |= (x & 1);
39+
x >>= 1;
40+
}
41+
return result;
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* ClearLeftmostSetBit class contains a method to clear the leftmost set bit of a number.
5+
* The leftmost set bit is the leftmost bit that is set to 1 in the binary representation of a number.
6+
*
7+
* Example:
8+
* 26 (11010) -> 10 (01010)
9+
* 1 (1) -> 0 (0)
10+
* 7 (111) -> 3 (011)
11+
* 6 (0110) -> 2 (0010)
12+
*
13+
* @author Hardvan
14+
*/
15+
public final class ClearLeftmostSetBit {
16+
private ClearLeftmostSetBit() {
17+
}
18+
19+
/**
20+
* Clears the leftmost set bit (1) of a given number.
21+
* Step 1: Find the position of the leftmost set bit
22+
* Step 2: Create a mask with all bits set except for the leftmost set bit
23+
* Step 3: Clear the leftmost set bit using AND with the mask
24+
*
25+
* @param num The input number.
26+
* @return The number after clearing the leftmost set bit.
27+
*/
28+
public static int clearLeftmostSetBit(int num) {
29+
int pos = 0;
30+
int temp = num;
31+
while (temp > 0) {
32+
temp >>= 1;
33+
pos++;
34+
}
35+
36+
int mask = ~(1 << (pos - 1));
37+
return num & mask;
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* CountLeadingZeros class contains a method to count the number of leading zeros in the binary representation of a number.
5+
* The number of leading zeros is the number of zeros before the leftmost 1 bit.
6+
* For example, the number 5 has 29 leading zeros in its 32-bit binary representation.
7+
* The number 0 has 32 leading zeros.
8+
* The number 1 has 31 leading zeros.
9+
* The number -1 has no leading zeros.
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class CountLeadingZeros {
14+
private CountLeadingZeros() {
15+
}
16+
17+
/**
18+
* Counts the number of leading zeros in the binary representation of a number.
19+
* Method: Keep shifting the mask to the right until the leftmost bit is 1.
20+
* The number of shifts is the number of leading zeros.
21+
*
22+
* @param num The input number.
23+
* @return The number of leading zeros.
24+
*/
25+
public static int countLeadingZeros(int num) {
26+
if (num == 0) {
27+
return 32;
28+
}
29+
30+
int count = 0;
31+
int mask = 1 << 31;
32+
while ((mask & num) == 0) {
33+
count++;
34+
mask >>>= 1;
35+
}
36+
37+
return count;
38+
}
39+
}
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+
/**
4+
* Gray code is a binary numeral system where two successive values differ in only one bit.
5+
* This is a simple conversion between binary and Gray code.
6+
* Example:
7+
* 7 -> 0111 -> 0100 -> 4
8+
* 4 -> 0100 -> 0111 -> 7
9+
* 0 -> 0000 -> 0000 -> 0
10+
* 1 -> 0001 -> 0000 -> 0
11+
* 2 -> 0010 -> 0011 -> 3
12+
* 3 -> 0011 -> 0010 -> 2
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class GrayCodeConversion {
17+
private GrayCodeConversion() {
18+
}
19+
20+
/**
21+
* Converts a binary number to Gray code.
22+
*
23+
* @param num The binary number.
24+
* @return The corresponding Gray code.
25+
*/
26+
public static int binaryToGray(int num) {
27+
return num ^ (num >> 1);
28+
}
29+
30+
/**
31+
* Converts a Gray code number back to binary.
32+
*
33+
* @param gray The Gray code number.
34+
* @return The corresponding binary number.
35+
*/
36+
public static int grayToBinary(int gray) {
37+
int binary = gray;
38+
while (gray > 0) {
39+
gray >>= 1;
40+
binary ^= gray;
41+
}
42+
return binary;
43+
}
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
5+
* Given two integers x and y, calculate the Hamming distance.
6+
* Example:
7+
* Input: x = 1, y = 4
8+
* Output: 2
9+
* Explanation: 1 (0001) and 4 (0100) have 2 differing bits.
10+
*
11+
* @author Hardvan
12+
*/
13+
public final class HammingDistance {
14+
private HammingDistance() {
15+
}
16+
17+
/**
18+
* Calculates the Hamming distance between two integers.
19+
* The Hamming distance is the number of differing bits between the two integers.
20+
*
21+
* @param x The first integer.
22+
* @param y The second integer.
23+
* @return The Hamming distance (number of differing bits).
24+
*/
25+
public static int hammingDistance(int x, int y) {
26+
int xor = x ^ y;
27+
return Integer.bitCount(xor);
28+
}
29+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* HigherLowerPowerOfTwo class has two methods to find the next higher and lower power of two.
5+
* <p>
6+
* nextHigherPowerOfTwo method finds the next higher power of two.
7+
* nextLowerPowerOfTwo method finds the next lower power of two.
8+
* Both methods take an integer as input and return the next higher or lower power of two.
9+
* If the input is less than 1, the next higher power of two is 1.
10+
* If the input is less than or equal to 1, the next lower power of two is 0.
11+
* nextHigherPowerOfTwo method uses bitwise operations to find the next higher power of two.
12+
* nextLowerPowerOfTwo method uses Integer.highestOneBit method to find the next lower power of two.
13+
* The time complexity of both methods is O(1).
14+
* The space complexity of both methods is O(1).
15+
* </p>
16+
*
17+
* @author Hardvan
18+
*/
19+
public final class HigherLowerPowerOfTwo {
20+
private HigherLowerPowerOfTwo() {
21+
}
22+
23+
/**
24+
* Finds the next higher power of two.
25+
*
26+
* @param x The given number.
27+
* @return The next higher power of two.
28+
*/
29+
public static int nextHigherPowerOfTwo(int x) {
30+
if (x < 1) {
31+
return 1;
32+
}
33+
x--;
34+
x |= x >> 1;
35+
x |= x >> 2;
36+
x |= x >> 4;
37+
x |= x >> 8;
38+
x |= x >> 16;
39+
return x + 1;
40+
}
41+
42+
/**
43+
* Finds the next lower power of two.
44+
*
45+
* @param x The given number.
46+
* @return The next lower power of two.
47+
*/
48+
public static int nextLowerPowerOfTwo(int x) {
49+
if (x < 1) {
50+
return 0;
51+
}
52+
return Integer.highestOneBit(x);
53+
}
54+
}

src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java

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

3+
import java.util.Arrays;
4+
35
/**
46
* Recursive Solution for 0-1 knapsack with memoization
57
* This method is basically an extension to the recursive approach so that we
@@ -15,10 +17,8 @@ int knapSack(int capacity, int[] weights, int[] profits, int numOfItems) {
1517
int[][] dpTable = new int[numOfItems + 1][capacity + 1];
1618

1719
// Loop to initially fill the table with -1
18-
for (int i = 0; i < numOfItems + 1; i++) {
19-
for (int j = 0; j < capacity + 1; j++) {
20-
dpTable[i][j] = -1;
21-
}
20+
for (int[] table : dpTable) {
21+
Arrays.fill(table, -1);
2222
}
2323

2424
return solveKnapsackRecursive(capacity, weights, profits, numOfItems, dpTable);
@@ -38,7 +38,6 @@ int solveKnapsackRecursive(int capacity, int[] weights, int[] profits, int numOf
3838
if (weights[numOfItems - 1] > capacity) {
3939
// Store the value of function call stack in table
4040
dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
41-
return dpTable[numOfItems][capacity];
4241
} else {
4342
// case 1. include the item, if it is less than the capacity
4443
final int includeCurrentItem = profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable);
@@ -48,7 +47,7 @@ int solveKnapsackRecursive(int capacity, int[] weights, int[] profits, int numOf
4847

4948
// Store the value of function call stack in table and return
5049
dpTable[numOfItems][capacity] = Math.max(includeCurrentItem, excludeCurrentItem);
51-
return dpTable[numOfItems][capacity];
5250
}
51+
return dpTable[numOfItems][capacity];
5352
}
5453
}

0 commit comments

Comments
 (0)