Minimum One Bit Operations to Make Integers Zero
HardUpdated: Aug 2, 2025
Practice on:
Problem
Given an integer n, you must transform it into 0 using the following operations any number of times:
- Change the rightmost (
0th) bit in the binary representation ofn. - Change the
ithbit in the binary representation ofnif the(i-1)thbit is set to1and the(i-2)ththrough0thbits are set to0.
Return the minimum number of operations to transform n into 0.
Examples
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 <= 10^9
Solution
Method 1 – Recursive Bit Manipulation (Gray Code)
Intuition
The problem is related to the minimum number of operations to convert n to 0 using Gray code properties. The operation count for each bit is recursive: flip the highest set bit, then recursively solve for the rest.
Approach
- If n == 0, return 0.
- Find the highest set bit position k in n.
- The answer is (1 << (k+1)) - 1 - minimumOneBitOperations(n ^ (1 << k)).
- Recursively solve for n ^ (1 << k).
Code
C++
class Solution {
public:
int minimumOneBitOperations(int n) {
if (n == 0) return 0;
int k = 31 - __builtin_clz(n);
return (1 << (k+1)) - 1 - minimumOneBitOperations(n ^ (1 << k));
}
};
Go
func minimumOneBitOperations(n int) int {
if n == 0 {
return 0
}
k := 0
for i := 31; i >= 0; i-- {
if n&(1<<i) > 0 {
k = i
break
}
}
return (1<<(k+1)) - 1 - minimumOneBitOperations(n^(1<<k))
}
Java
class Solution {
public int minimumOneBitOperations(int n) {
if (n == 0) return 0;
int k = 31 - Integer.numberOfLeadingZeros(n);
return (1 << (k+1)) - 1 - minimumOneBitOperations(n ^ (1 << k));
}
}
Kotlin
class Solution {
fun minimumOneBitOperations(n: Int): Int {
if (n == 0) return 0
val k = 31 - Integer.numberOfLeadingZeros(n)
return (1 shl (k+1)) - 1 - minimumOneBitOperations(n xor (1 shl k))
}
}
Python
class Solution:
def minimumOneBitOperations(self, n: int) -> int:
if n == 0:
return 0
k = n.bit_length() - 1
return (1 << (k+1)) - 1 - self.minimumOneBitOperations(n ^ (1 << k))
Rust
impl Solution {
pub fn minimum_one_bit_operations(n: i32) -> i32 {
if n == 0 {
return 0;
}
let k = 31 - n.leading_zeros();
(1 << (k+1)) - 1 - Solution::minimum_one_bit_operations(n ^ (1 << k))
}
}
TypeScript
class Solution {
minimumOneBitOperations(n: number): number {
if (n === 0) return 0;
let k = 31;
while (k >= 0 && ((n >> k) & 1) === 0) k--;
return (1 << (k+1)) - 1 - this.minimumOneBitOperations(n ^ (1 << k));
}
}
Complexity
- ⏰ Time complexity:
O(log n)— Each recursion reduces the highest bit. - 🧺 Space complexity:
O(log n)— For recursion stack.