1611. Minimum One Bit Operations to Make Integers Zero
Difficulty: Hard
Topics: Dynamic Programming, Bit Manipulation, Memoization, Weekly Contest 209
Given an integer n, you must transform it into 0 using the following operations any number of times:
- Change the rightmost (
0ᵗʰ) bit in the binary representation ofn. - Change the
iᵗʰbit in the binary representation ofnif the(i-1)ᵗʰbit is set to1and the(i-2)ᵗʰthrough 0th bits are set to0.
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 2ⁿᵈ operation since the 0ᵗʰ bit is 1.
- "01" -> "00" with the 1ˢᵗ operation.
Example 2:
- Input: n = 6
- Output: 4
-
Explanation: The binary representation of 6 is "110".
- "110" -> "010" with the 2ⁿᵈ operation since the 1ˢᵗ bit is 1 and 0ᵗʰ through 0ᵗʰ bits are 0.
- "010" -> "011" with the 1ˢᵗ operation.
- "011" -> "001" with the 2ⁿᵈ operation since the 0ᵗʰ bit is 1.
- "001" -> "000" with the 1ˢᵗ operation.
Constraints:
0 <= n <= 10⁹
Hint:
- The fastest way to convert n to zero is to remove all set bits starting from the leftmost one. Try some simple examples to learn the rule of how many steps are needed to remove one set bit.
- consider n=2ᵏ case first, then solve for all n.
Solution:
We need to determine the minimum number of operations required to transform a given integer n into 0 using specific bit manipulation operations. The operations allowed are changing the rightmost bit (0ᵗʰ bit) or changing the ith bit (for i ≥ 1) under the condition that the (i-1)ᵗʰ bit is set to 1 and all bits from (i-2) down to 0 are set to 0.
Approach:
Problem Analysis: The problem involves transforming an integer to zero by flipping bits according to specific rules. The key insight is recognizing that the minimum number of operations required to reduce
nto0is equivalent to computing the inverse Gray code ofn. The Gray code is a binary numeral system where two successive values differ in only one bit. The inverse Gray code transformation converts a Gray code back to its original binary representation, which aligns perfectly with the operations allowed in this problem.Key Insight: The minimum operations needed to convert
nto0can be derived by continuously XORingnwith itself right-shifted untilnbecomes zero. This process effectively computes the inverse Gray code ofn.Algorithm Selection: We use a loop to iteratively compute the XOR of
nwith its right-shifted value untilnreduces to zero. The accumulated XOR result gives the minimum operations required.
Let's implement this solution in PHP: 1611. Minimum One Bit Operations to Make Integers Zero
<?php
/**
* @param Integer $n
* @return Integer
*/
function minimumOneBitOperations($n) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo minimumOneBitOperations(3) . PHP_EOL; // expected 2
echo minimumOneBitOperations(6) . PHP_EOL; // expected 4
echo minimumOneBitOperations(0) . PHP_EOL; // 0
echo minimumOneBitOperations(2) . PHP_EOL; // 3
echo minimumOneBitOperations(4) . PHP_EOL; // 7
?>
Explanation:
-
Initialization: Start with
$resultinitialized to0. -
Loop Until
nis Zero: In each iteration, XOR the current value ofnwith$result. -
Right Shift
n: Right shiftnby one bit to process the next bit in the subsequent iteration. -
Result: The accumulated
$resultafter the loop ends is the minimum number of operations required to transformnto0.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)