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 <= 109Problem Overview: Given an integer n, compute the minimum number of operations required to transform it to 0. The operation flips the least significant bit or flips the i-th bit only when the (i−1)-th bit is 1 and all lower bits are 0. The optimal solution relies on the relationship between these operations and Gray code transformations.
Approach 1: Recursive + Memoization (O(log n) time, O(log n) space)
This approach models the problem using recursion based on the highest set bit. Let k be the position of the most significant bit in n. To clear that bit, you must first transform the lower bits into a specific pattern, flip the bit, then revert the pattern. The recurrence becomes f(n) = (1 << (k + 1)) - 1 - f(n ^ (1 << k)). Memoization stores previously computed values to avoid recomputation. The recursion depth is proportional to the number of bits, giving O(log n) time.
This method fits naturally with dynamic programming and memoization. It mirrors how the operation sequence builds from smaller subproblems.
Approach 2: Iterative Bit Manipulation (Gray Code Insight) (O(log n) time, O(1) space)
The minimum operations correspond to converting a Gray code number back to its binary index. If you repeatedly XOR the current value with itself shifted right, you effectively compute the inverse Gray code. Start with ans = 0. While n > 0, perform ans ^= n and shift n >>= 1. Each iteration removes one bit from consideration.
This works because the operation rules mimic Gray code transitions where only one bit changes between states. The XOR accumulation reconstructs the total number of steps needed to reach zero. The algorithm processes each bit once, giving O(log n) time with constant memory using bit manipulation.
Recommended for interviews: The iterative bit-manipulation solution is what most interviewers expect. It demonstrates recognition of the Gray code pattern and produces a concise O(log n), O(1) solution. The recursive DP version is useful for explaining the underlying recurrence and showing how the pattern emerges.
The idea is to use a recursive function to determine the minimum number of operations needed to reduce the given number n to 0. Consider each bit change step as a recursive call, effectively exploring the tree of possibilities until we reach 0.
We need to account for the operation rules defined in the problem, particularly handling the change in the i-th bit when needed.
This function computes the recursive solution. It checks the highest set bit and recurses by flipping the bit using XOR and adjusting the metric accordingly. The base case is when n is 0.
Time Complexity: O(log n), where n is the input size.
Space Complexity: O(log n), due to the recursion stack.
This approach removes recursion and implements the bit operations iteratively. The key step involves simulating the recursive logic using a while loop and maintaining a transformation count.
The iterative loop continuously XORs res with current num and shifts num right, mimicking recursive pattern iteratively.
Time Complexity: O(log n)
Space Complexity: O(1)
This problem essentially asks for the inverse transformation of Gray code at position n, i.e., constructing the original number from the Gray code.
Let's first review how to convert binary code to binary Gray code. The rule is to keep the most significant bit of the binary code as the most significant bit of the Gray code, while the second most significant bit of the Gray code is obtained by XORing the most significant bit and the second most significant bit of the binary code. The remaining bits of the Gray code are computed similarly to the second most significant bit.
Suppose a binary number is represented as B_{n-1}B_{n-2}...B_2B_1B_0, and its Gray code representation is G_{n-1}G_{n-2}...G_2G_1G_0. The most significant bit is kept, so G_{n-1} = B_{n-1}; and for other bits G_i = B_{i+1} \oplus B_{i}, where i=0,1,2..,n-2.
So what is the inverse transformation from Gray code to binary code?
We can observe that the most significant bit of the Gray code is kept, so B_{n-1} = G_{n-1}; and B_{n-2} = G_{n-2} \oplus B_{n-1} = G_{n-2} \oplus G_{n-1}; and for other bits B_i = G_{i} \oplus G_{i+1} cdots \oplus G_{n-1}, where i=0,1,2..,n-2. Therefore, we can use the following function rev(x) to obtain its binary code:
int rev(int x) {
int n = 0;
for (; x != 0; x >>= 1) {
n ^= x;
}
return n;
}
The time complexity is O(log n), where n is the integer given in the problem. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Recursive Approach | Time Complexity: O(log n), where n is the input size. |
| Iterative Approach Using Bit Manipulation | Time Complexity: O(log n) |
| Gray Code Inverse Transform (Gray Code to Binary Code) | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive with Memoization | O(log n) | O(log n) | When explaining the recurrence or building intuition from subproblems |
| Iterative Bit Manipulation (Inverse Gray Code) | O(log n) | O(1) | Best practical and interview solution; minimal code and constant memory |
Minimum One Bit Operations to Make Integers Zero | Detailed Explanation | Leetcode 1611 | MIK • codestorywithMIK • 14,365 views views
Watch 9 more video solutions →Practice Minimum One Bit Operations to Make Integers Zero with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor