1611. Minimum One Bit Operations to Make Integers Zero (LeetCode) (solution: C++, C#)

 

Problem Name: 1611. Minimum One Bit Operations to Make Integers Zero
Problem Link: https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/
Difficulty: Hrad
Tag: Dynamic Programming | Bit Manipulation | Memoization
Language: C# | C++
OJ: LeetCode

Algorithm:
Base Case Check:
If the input n is 0, the minimum number of one-bit operations is 0, as there are no bits to toggle.
Recursive Step:
For non-zero n, the algorithm toggles the least significant bit by performing a bitwise XOR (^) with the result of the recursive call on the right-shifted value of n by 1 (n >> 1).
Bitwise XOR Operation:
The XOR operation is used to toggle the least significant bit of n. It flips the bit if it's 1 and leaves it unchanged if it's 0.
Recursion Termination:
The recursion continues until the base case is reached (when n becomes 0). At each step, the algorithm toggles the least significant bit.

Code(C#)
public class Solution
 {
     public int MinimumOneBitOperations(int n)
     {
          if (n == 0) return 0;
          return n ^ MinimumOneBitOperations(n >> 1);
     }
 }

Code(C++)
Not done yet