Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.

Given a number, we need to find the length of the longest consecutive 1's in its binary representation. This problem can be solved using bit manipulation techniques.

Example

Input: n = 15
Output: 4
The binary representation of 15 is 1111.

Algorithm

The algorithm uses bit manipulation to count consecutive 1's:

Step 1: Input the number
Step 2: Use a counter variable to track iterations
Step 3: Apply bitwise AND with left-shifted number
Step 4: This reduces each sequence of 1's by one in each iteration
Step 5: Count iterations until the number becomes 0

Using Bit Manipulation

This method uses the property that n & (n removes the rightmost 1 from each sequence of consecutive 1's ?

def maxlength(n):
    # Initialize result
    count = 0
    
    # Count the number of iterations to reach n = 0
    while n != 0:
        # This operation reduces length of every sequence of 1s by one
        n = n & (n << 1)
        count += 1
    
    return count

# Test the function
n = 15
result = maxlength(n)
print(f"Number: {n}")
print(f"Binary: {bin(n)}")
print(f"Maximum Length of consecutive 1's: {result}")
Number: 15
Binary: 0b1111
Maximum Length of consecutive 1's: 4

How It Works

Let's trace through the algorithm with n = 15 (binary: 1111):

def trace_algorithm(n):
    print(f"Initial: {n} (binary: {bin(n)})")
    count = 0
    
    while n != 0:
        count += 1
        left_shift = n << 1
        n = n & left_shift
        print(f"Iteration {count}: n & (n << 1) = {n} (binary: {bin(n) if n > 0 else '0b0'})")
    
    return count

# Trace with example
result = trace_algorithm(15)
print(f"Total iterations (max consecutive 1's): {result}")
Initial: 15 (binary: 0b1111)
Iteration 1: n & (n << 1) = 14 (binary: 0b1110)
Iteration 2: n & (n << 1) = 12 (binary: 0b1100)
Iteration 3: n & (n << 1) = 8 (binary: 0b1000)
Iteration 4: n & (n << 1) = 0 (binary: 0b0)
Total iterations (max consecutive 1's): 4

Alternative Approach Using String Conversion

A simpler approach converts the number to binary string and finds the longest sequence ?

def maxlength_string(n):
    # Convert to binary and remove '0b' prefix
    binary_str = bin(n)[2:]
    
    max_count = 0
    current_count = 0
    
    for bit in binary_str:
        if bit == '1':
            current_count += 1
            max_count = max(max_count, current_count)
        else:
            current_count = 0
    
    return max_count

# Test with different numbers
test_numbers = [15, 14, 13, 7]

for num in test_numbers:
    result1 = maxlength(num)
    result2 = maxlength_string(num)
    print(f"Number: {num}, Binary: {bin(num)}, Max consecutive 1's: {result1}")
Number: 15, Binary: 0b1111, Max consecutive 1's: 4
Number: 14, Binary: 0b1110, Max consecutive 1's: 3
Number: 13, Binary: 0b1101, Max consecutive 1's: 2
Number: 7, Binary: 0b111, Max consecutive 1's: 3

Conclusion

The bit manipulation approach efficiently finds consecutive 1's by using n & (n operation. The string-based approach is more intuitive but less efficient for large numbers.

Updated on: 2026-03-24T20:59:31+05:30

529 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements