Given an integer n
, you must transform it into 0
using the following operations any number of times:
0th
) bit in the binary representation of n
.ith
bit in the binary representation of n
if the (i-1)th
bit is set to 1
and the (i-2)th
through 0th
bits are set to 0
.Return the minimum number of operations to transform n
into 0
.
Example 1:
Input: n = 3 Output: 2 Explanation: The binary representation of 3 is "11". "11" -> "01" with the 2nd operation since the 0th bit is 1. "01" -> "00" with the 1st operation.
Example 2:
Input: n = 6 Output: 4 Explanation: The binary representation of 6 is "110". "110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0. "010" -> "011" with the 1st operation. "011" -> "001" with the 2nd operation since the 0th bit is 1. "001" -> "000" with the 1st operation.
Constraints:
0 <= n <= 109
This problem asks for the minimum number of operations to transform a given integer n
into 0 using two specific bit manipulation operations. The optimal solution leverages bitwise XOR (^
) in a clever way.
The core idea is based on the observation that the operations can be seen as flipping bits. Let's analyze the operations:
Change the rightmost bit: This is a simple flip of the least significant bit (LSB).
Change the ith bit: This operation is only possible if the (i-1)th bit is 1, and all bits from (i-2)th down to 0th are 0. This essentially means you can flip a bit only if the bit to its immediate left is 1 and all bits to its right are 0.
Instead of directly simulating these operations, we can use bitwise XOR to achieve the same effect more efficiently. The solution cleverly uses a loop (iterative approach) or recursion to achieve the minimum operations.
Iterative Approach (Solution 1):
The iterative approach directly computes the minimum operations using a while
loop and XOR. The loop continues as long as n
is greater than 0. In each iteration:
ans ^= n
: This line performs the XOR operation between the current ans
(initially 0) and n
. This effectively flips bits in ans
based on the bits in n
.
n >>= 1
: This right-shifts n
by 1 bit, effectively discarding the least significant bit.
This process repeats until n
becomes 0, and the final value of ans
represents the minimum number of operations.
Recursive Approach (Solution 2):
The recursive approach is a more elegant but potentially less efficient way to solve the problem. It leverages the property that the minimum number of operations for n
is related to the minimum number of operations for n >> 1
. The recursive relation is:
minimumOneBitOperations(n) = n ^ minimumOneBitOperations(n >> 1)
The base case is when n
is 0, returning 0 operations.
Solution 1 (Iterative):
n
.Solution 2 (Recursive):
The code examples for both approaches are provided in the problem description. The iterative approach is generally preferred due to its better space complexity. The recursive solution is more concise but can be less efficient for very large values of n
due to the function call overhead.
The problem demonstrates a clever application of bitwise XOR to efficiently solve a seemingly complex bit manipulation problem. The iterative solution offers the best balance of time and space efficiency, while the recursive solution provides an alternative perspective on the problem's structure. Both approaches achieve the correct solution in logarithmic time.