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
Check if bits of a number has count of consecutive set bits in increasing order in Python
When working with binary representations, we sometimes need to check if consecutive set bits (1s) appear in increasing order. This problem asks us to verify whether groups of continuous 1s in a number's binary representation have counts that increase from left to right.
For example, if we have n = 1775, its binary representation is 11011101111. The groups of consecutive 1s are [2, 3, 4], which are in increasing order, so the result is True.
Algorithm Steps
To solve this problem, we follow these steps:
- Convert the number to its binary representation
- Count consecutive groups of 1s from left to right
- Check if each group size is greater than the previous group
- Return True if all groups are in increasing order, False otherwise
Implementation
def solve(n):
# Convert number to binary string (remove '0b' prefix)
binary_str = bin(n)[2:]
groups = []
current_count = 0
# Count consecutive 1s
for bit in binary_str:
if bit == '1':
current_count += 1
else:
if current_count > 0:
groups.append(current_count)
current_count = 0
# Add the last group if it ends with 1s
if current_count > 0:
groups.append(current_count)
# Check if groups are in increasing order
for i in range(1, len(groups)):
if groups[i] <= groups[i-1]:
return False
return True
# Test with the example
n = 1775
print(f"Binary of {n}: {bin(n)[2:]}")
print(f"Result: {solve(n)}")
Binary of 1775: 11011101111 Result: True
Step-by-Step Breakdown
Let's trace through the example with n = 1775:
def solve_with_trace(n):
binary_str = bin(n)[2:]
print(f"Binary representation: {binary_str}")
groups = []
current_count = 0
for i, bit in enumerate(binary_str):
if bit == '1':
current_count += 1
print(f"Position {i}: Found '1', current count = {current_count}")
else:
if current_count > 0:
groups.append(current_count)
print(f"Position {i}: Found '0', added group of size {current_count}")
current_count = 0
if current_count > 0:
groups.append(current_count)
print(f"Final group of size {current_count}")
print(f"Groups of consecutive 1s: {groups}")
# Check if increasing
for i in range(1, len(groups)):
if groups[i] <= groups[i-1]:
print(f"Group {groups[i]} is not greater than {groups[i-1]}")
return False
print("All groups are in increasing order!")
return True
n = 1775
result = solve_with_trace(n)
print(f"Final result: {result}")
Binary representation: 11011101111 Position 0: Found '1', current count = 1 Position 1: Found '1', current count = 2 Position 2: Found '0', added group of size 2 Position 3: Found '1', current count = 1 Position 4: Found '1', current count = 2 Position 5: Found '1', current count = 3 Position 6: Found '0', added group of size 3 Position 7: Found '1', current count = 1 Position 8: Found '1', current count = 2 Position 9: Found '1', current count = 3 Position 10: Found '1', current count = 4 Final group of size 4 Groups of consecutive 1s: [2, 3, 4] All groups are in increasing order! Final result: True
Testing with Different Cases
def test_cases():
test_numbers = [1775, 7, 15, 31, 119]
for num in test_numbers:
binary_rep = bin(num)[2:]
result = solve(num)
print(f"n = {num}, binary = {binary_rep}, result = {result}")
test_cases()
n = 1775, binary = 11011101111, result = True n = 7, binary = 111, result = True n = 15, binary = 1111, result = True n = 31, binary = 11111, result = True n = 119, binary = 1110111, result = False
Conclusion
This algorithm efficiently checks if consecutive groups of 1s in a binary representation are in increasing order. The solution converts the number to binary, counts consecutive 1s, and verifies the increasing pattern with O(log n) time complexity.
