Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Program to find higher number with same number of set bits as n in Python?
Given a number n, we need to find the smallest next higher number that has the same number of set bits (1s) in its binary representation.
For example, if n = 7 (binary: 0111), the next higher number with three 1s is 11 (binary: 1011).
Algorithm
The approach involves bit manipulation to rearrange the bits ?
- Count trailing zeros and ones from the rightmost bit
- Find the rightmost non-trailing zero and flip it to 1
- Clear all bits to the right of this position
- Set the required number of 1s at the rightmost positions
Example
class Solution:
def solve(self, n):
copy = n
zeros = 0
ones = 0
# Count trailing zeros
while copy and not copy & 1:
zeros += 1
copy >>= 1
# Count trailing ones after zeros
while copy & 1:
ones += 1
copy >>= 1
# Position of rightmost non-trailing zero
right = ones + zeros
# Flip the rightmost non-trailing zero
n |= 1 << right
# Clear all bits to the right of 'right'
n &= ~((1 << right) - 1)
# Insert (ones-1) ones on the right
n |= (1 << (ones - 1)) - 1
return n
# Test the solution
ob = Solution()
n = 7
result = ob.solve(n)
print(f"Input: {n} (binary: {bin(n)})")
print(f"Output: {result} (binary: {bin(result)})")
Input: 7 (binary: 0b111) Output: 11 (binary: 0b1011)
How It Works
Let's trace through the algorithm with n = 7 (binary: 111) ?
def trace_algorithm(n):
print(f"Original number: {n} (binary: {bin(n)})")
copy = n
zeros = ones = 0
# Count trailing zeros
while copy and not copy & 1:
zeros += 1
copy >>= 1
print(f"Trailing zeros: {zeros}")
# Count trailing ones
while copy & 1:
ones += 1
copy >>= 1
print(f"Trailing ones: {ones}")
right = ones + zeros
print(f"Position to flip: {right}")
# Step by step transformation
n |= 1 << right
print(f"After flipping bit: {n} (binary: {bin(n)})")
n &= ~((1 << right) - 1)
print(f"After clearing right bits: {n} (binary: {bin(n)})")
n |= (1 << (ones - 1)) - 1
print(f"Final result: {n} (binary: {bin(n)})")
return n
trace_algorithm(7)
Original number: 7 (binary: 0b111) Trailing zeros: 0 Trailing ones: 3 Position to flip: 3 After flipping bit: 15 (binary: 0b1111) After clearing right bits: 8 (binary: 0b1000) Final result: 11 (binary: 0b1011)
Multiple Examples
def find_next_higher(n):
copy = n
zeros = ones = 0
while copy and not copy & 1:
zeros += 1
copy >>= 1
while copy & 1:
ones += 1
copy >>= 1
right = ones + zeros
n |= 1 << right
n &= ~((1 << right) - 1)
n |= (1 << (ones - 1)) - 1
return n
# Test with different numbers
test_cases = [7, 12, 6, 13]
for num in test_cases:
result = find_next_higher(num)
print(f"n={num} ({bin(num)}) ? {result} ({bin(result)})")
n=7 (0b111) ? 11 (0b1011) n=12 (0b1100) ? 17 (0b10001) n=6 (0b110) ? 9 (0b1001) n=13 (0b1101) ? 14 (0b1110)
Conclusion
This algorithm efficiently finds the next higher number with the same number of set bits using bit manipulation. The key insight is identifying the rightmost non-trailing zero, flipping it, and rearranging the remaining bits optimally.
Advertisements
