Program to find minimum one bit operations to make integers zero in Python

We need to transform a number n into 0 using specific bit operations. The operations allowed are:

  • Select the rightmost bit in the binary representation of n.

  • Change the ith bit in the binary representation of n when the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0.

We need to find the minimum number of operations required to transform n into 0.

Understanding the Problem

For example, if n = 6, the binary representation is "110". The transformation steps are ?

  • Start: "110" (6 in decimal)

  • Apply operation 2: "010" (2 in decimal)

  • Apply operation 1: "011" (3 in decimal)

  • Apply operation 2: "001" (1 in decimal)

  • Apply operation 1: "000" (0 in decimal)

Total operations: 4

Algorithm

To solve this problem, we follow these steps ?

  • Convert n to binary representation as a list of bits

  • Process each bit from left to right

  • Keep track of the last processed bit

  • If the last bit was 1, flip the current bit

  • Convert the result back to decimal

Example

def solve(n):
    # Convert number to binary and create list of bits
    binary_bits = list(map(int, bin(n)[2:]))
    result_bits = []
    last_bit = 0
    
    for bit in binary_bits:
        # If last bit was 1, flip current bit
        if last_bit == 1:
            bit = 1 - bit
        last_bit = bit
        result_bits.append(bit)
    
    # Convert result back to binary string then to decimal
    binary_string = ''.join(map(str, result_bits))
    return int(binary_string, 2)

# Test with example
n = 6
print(f"Input: {n}")
print(f"Binary: {bin(n)[2:]}")
print(f"Minimum operations: {solve(n)}")
Input: 6
Binary: 110
Minimum operations: 4

Step-by-Step Trace

Let's trace through the algorithm for n = 6 ("110") ?

def solve_with_trace(n):
    binary_bits = list(map(int, bin(n)[2:]))
    print(f"Original binary: {binary_bits}")
    
    result_bits = []
    last_bit = 0
    
    for i, bit in enumerate(binary_bits):
        original_bit = bit
        if last_bit == 1:
            bit = 1 - bit
        last_bit = bit
        result_bits.append(bit)
        print(f"Step {i+1}: bit={original_bit}, last_bit={last_bit}, result={result_bits}")
    
    binary_string = ''.join(map(str, result_bits))
    decimal_result = int(binary_string, 2)
    print(f"Final binary: {binary_string}")
    print(f"Final decimal: {decimal_result}")
    return decimal_result

n = 6
solve_with_trace(n)
Original binary: [1, 1, 0]
Step 1: bit=1, last_bit=1, result=[1]
Step 2: bit=1, last_bit=0, result=[1, 0]
Step 3: bit=0, last_bit=0, result=[1, 0, 0]
Final binary: 100
Final decimal: 4

Testing with Different Values

def solve(n):
    binary_bits = list(map(int, bin(n)[2:]))
    result_bits = []
    last_bit = 0
    
    for bit in binary_bits:
        if last_bit == 1:
            bit = 1 - bit
        last_bit = bit
        result_bits.append(bit)
    
    binary_string = ''.join(map(str, result_bits))
    return int(binary_string, 2)

# Test with multiple values
test_cases = [6, 8, 15, 3]

for n in test_cases:
    result = solve(n)
    print(f"n = {n}, binary = {bin(n)[2:]}, operations = {result}")
n = 6, binary = 110, operations = 4
n = 8, binary = 1000, operations = 8
n = 15, binary = 1111, operations = 8
n = 3, binary = 11, operations = 2

Conclusion

The algorithm transforms the binary representation by flipping bits based on the previous bit value. The minimum number of operations equals the decimal value of the transformed binary string.

---
Updated on: 2026-03-26T14:28:19+05:30

399 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements