Watch 10 video solutions for Minimum One Bit Operations to Make Integers Zero, a hard level problem involving Dynamic Programming, Bit Manipulation, Memoization. This walkthrough by codestorywithMIK has 14,365 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |