Add NextHigherSameBitCount algorithm (#5865)

This commit is contained in:
Hardik Pawar
2024-10-26 20:01:11 +05:30
committed by GitHub
parent 9e1dd86a08
commit 94daff0895
3 changed files with 54 additions and 0 deletions

View File

@ -41,6 +41,7 @@
* [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java)
* [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java)
* [ModuloPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwo.java)
* [NextHigherSameBitCount](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NextHigherSameBitCount.java)
* [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java)
* [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java)
* [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java)
@ -744,6 +745,7 @@
* [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java)
* [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java)
* [ModuloPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ModuloPowerOfTwoTest.java)
* [NextHigherSameBitCountTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NextHigherSameBitCountTest.java)
* [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java)
* [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java)
* [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java)

View File

@ -0,0 +1,30 @@
package com.thealgorithms.bitmanipulation;
/**
* This class provides a method to find the next higher number
* with the same number of set bits as the given number.
*
* @author Hardvan
*/
public final class NextHigherSameBitCount {
private NextHigherSameBitCount() {
}
/**
* Finds the next higher integer with the same number of set bits.
* Steps:
* 1. Find {@code c}, the rightmost set bit of {@code n}.
* 2. Find {@code r}, the rightmost set bit of {@code n + c}.
* 3. Swap the bits of {@code r} and {@code n} to the right of {@code c}.
* 4. Shift the bits of {@code r} and {@code n} to the right of {@code c} to the rightmost.
* 5. Combine the results of steps 3 and 4.
*
* @param n the input number
* @return the next higher integer with the same set bit count
*/
public static int nextHigherSameBitCount(int n) {
int c = n & -n;
int r = n + c;
return (((r ^ n) >> 2) / c) | r;
}
}

View File

@ -0,0 +1,22 @@
package com.thealgorithms.bitmanipulation;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class NextHigherSameBitCountTest {
@ParameterizedTest
@CsvSource({
"5, 6", // 101 -> 110
"7, 11", // 0111 -> 1011
"3, 5", // 011 -> 101
"12, 17", // 001100 -> 010001
"15, 23" // 01111 -> 10111
})
void
testNextHigherSameBitCount(int input, int expected) {
assertEquals(expected, NextHigherSameBitCount.nextHigherSameBitCount(input));
}
}