Given an integer n, find the next biggest integer with the same number of 1-bits on. For example, given the number 6 (0110 in binary), return 9 (1001).
Input: 12Output: 17Explanation:
12in binary:1100(two 1-bits)17in binary:10001(two 1-bits)17is the next bigger integer with the same number of 1-bits as 12.
Input: 7Output: 11Explanation:
7in binary:0111(three 1-bits)11in binary:1011(three 1-bits)11is the next bigger integer with the same number of 1-bits as 7.
To find the next bigger number with the same number of 1-bits, we need to rearrange the bits. The key insight is to find the rightmost 0-bit that has 1-bits to its right, flip it to 1, then rearrange the remaining bits to the right to get the smallest possible number. This involves finding the rightmost “01” pattern, flipping it to “10”, then moving all remaining 1-bits as far right as possible.
classSolution {
public:int nextBiggerSameBits(int n) {
int c = n;
int c0 =0; // Count of trailing zeros
int c1 =0; // Count of ones to the right of trailing zeros
// Count trailing zeros
while (((c &1) ==0) && c !=0) {
c0++;
c >>=1;
}
// Count ones after trailing zeros
while ((c &1) ==1) {
c1++;
c >>=1;
}
// No bigger number with same number of 1's
if (c0 + c1 ==31|| c0 + c1 ==0) {
return-1;
}
int p = c0 + c1; // Position of rightmost non-trailing zero
n |= (1<< p); // Flip the rightmost non-trailing zero
n &=~((1<< p) -1); // Clear all bits to the right of p
n |= (1<< (c1 -1)) -1; // Insert (c1-1) ones on the right
return n;
}
};
funcnextBiggerSameBits(nint) int {
c:=nc0:=0// Count of trailing zerosc1:=0// Count of ones to the right of trailing zeros// Count trailing zerosfor (c&1) ==0&&c!=0 {
c0++c>>=1 }
// Count ones after trailing zerosfor (c&1) ==1 {
c1++c>>=1 }
// No bigger number with same number of 1'sifc0+c1==31||c0+c1==0 {
return-1 }
p:=c0+c1// Position of rightmost non-trailing zeron|= (1<<p) // Flip the rightmost non-trailing zeron&= ^((1<<p) -1) // Clear all bits to the right of pn|= (1<< (c1-1)) -1// Insert (c1-1) ones on the rightreturnn}
classSolution {
publicintnextBiggerSameBits(int n) {
int c = n;
int c0 = 0; // Count of trailing zerosint c1 = 0; // Count of ones to the right of trailing zeros// Count trailing zeroswhile (((c & 1) == 0) && c != 0) {
c0++;
c >>>= 1;
}
// Count ones after trailing zeroswhile ((c & 1) == 1) {
c1++;
c >>>= 1;
}
// No bigger number with same number of 1'sif (c0 + c1 == 31 || c0 + c1 == 0) {
return-1;
}
int p = c0 + c1; // Position of rightmost non-trailing zero n |= (1 << p); // Flip the rightmost non-trailing zero n &=~((1 << p) - 1); // Clear all bits to the right of p n |= (1 << (c1 - 1)) - 1; // Insert (c1-1) ones on the rightreturn n;
}
}
classSolution:
defnext_bigger_same_bits(self, n: int) -> int:
c = n
c0 =0# Count of trailing zeros c1 =0# Count of ones to the right of trailing zeros# Count trailing zeroswhile (c &1) ==0and c !=0:
c0 +=1 c >>=1# Count ones after trailing zeroswhile (c &1) ==1:
c1 +=1 c >>=1# No bigger number with same number of 1'sif c0 + c1 ==31or c0 + c1 ==0:
return-1 p = c0 + c1 # Position of rightmost non-trailing zero n |= (1<< p) # Flip the rightmost non-trailing zero n &=~((1<< p) -1) # Clear all bits to the right of p n |= (1<< (c1 -1)) -1# Insert (c1-1) ones on the rightreturn n
We can treat this as finding the next lexicographical permutation of the binary representation. Convert the number to a bit array, find the next permutation using the standard algorithm, then convert back to integer. This approach is more intuitive but less efficient than direct bit manipulation.
A straightforward approach is to increment the number and check if it has the same number of 1-bits. While inefficient, this method is simple to understand and implement. We use built-in bit counting functions to compare the number of set bits.
funccountBits(nint) int {
count:=0forn > 0 {
count+=n&1n>>=1 }
returncount}
funcnextBiggerBruteForce(nint) int {
originalBits:=countBits(n)
fori:=n+1; i > n; i++ { // Check for overflowifcountBits(i) ==originalBits {
returni }
}
return-1// No solution found (overflow case)}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
privateintcountBits(int n) {
return Integer.bitCount(n);
}
publicintnextBiggerBruteForce(int n) {
int originalBits = countBits(n);
for (int i = n + 1; i > n; i++) { // Check for overflowif (countBits(i) == originalBits) {
return i;
}
}
return-1; // No solution found (overflow case) }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defcount_bits(self, n: int) -> int:
return bin(n).count('1')
defnext_bigger_brute_force(self, n: int) -> int:
original_bits = self.count_bits(n)
i = n +1while i > n: # Check for overflowif self.count_bits(i) == original_bits:
return i
i +=1return-1# No solution found (overflow case)