# Minimum number of given operations required to be performed to reduce N to 0

• Difficulty Level : Hard
• Last Updated : 10 Aug, 2021

Given an integer N, the task is to reduce N to 0 in the minimum number of operations using the following operations any number of times:

• Change the rightmost (0th) bit in the binary representation of N.
• Change the 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.

Examples:

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.

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.

Approach: The idea is based on the following observations:

• 1 → 0 needs 1 operation.
2 → 0 i.e 10 → 11 → 01 → 0 needs 3 operations.
4 → 0 i.e 100 → 101 → 111 → 110 → 010 → 11 → 01 →0 needs 7 operations.
Hence, it can be generalized for any power of 2 i.e, 2k needs 2(k+1) -1 operations.
• If a → b requires k operation, b → a also requires k operation.

Intermediate numbers from 2n to 2(n-1) contain all numbers between 2n and 2(n-1), which demonstrates that any given non-negative integer can be converted to 0. Let f(n) be a function to find the minimum number of operations required. The recurrence relation is:
f(n) = f(2k) – f(n xor 2k), where k = next bit of most significant bit of n.

The idea is to use recursion. Follow the steps below to solve the problem:

• Create a recursive function that takes a number N as a parameter.
• If the value of N is equal to 0, return 0.
• Else, find the highest power of 2 less than or equal to N and store it in a variable X.
• Store the value returned by recursively calling the function for (X^(X/2)^N) in a variable S.
• Add the value of X to S and return it.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to find minimum` `// operations required to convert` `// N to 0` `int` `minimumOneBitOperations(``int` `n, ``int` `res = 0)` `{` `    ``// Base Case` `    ``if` `(n == 0)` `        ``return` `res;`   `    ``// Store the highest power of 2` `    ``// less than or equal to n` `    ``int` `b = 1;` `    ``while` `((b << 1) <= n)` `        ``b = b << 1;`   `    ``// Return the result` `    ``return` `minimumOneBitOperations(` `        ``(b >> 1) ^ b ^ n, res + b);` `}`   `// Driver Code` `int` `main()` `{` `    ``// Given Input` `    ``int` `N = 6;`   `    ``// Function call` `    ``cout << minimumOneBitOperations(N);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `class` `GFG {`   `    ``// Function to find minimum` `    ``// operations required to convert` `    ``// N to 0` `    ``public` `static` `int` `minimumOneBitOperations(``int` `n, ``int` `res) ` `    ``{` `      `  `        ``// Base Case` `        ``if` `(n == ``0``)` `            ``return` `res;`   `        ``// Store the highest power of 2` `        ``// less than or equal to n` `        ``int` `b = ``1``;` `        ``while` `((b << ``1``) <= n)` `            ``b = b << ``1``;`   `        ``// Return the result` `        ``return` `minimumOneBitOperations((b >> ``1``) ^ b ^ n, res + b);` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String args[])` `    ``{` `      `  `        ``// Given Input` `        ``int` `N = ``6``;`   `        ``// Function call` `        ``System.out.println(minimumOneBitOperations(N, ``0``));` `    ``}` `}`   `// This code is contributed by gfgking.`

## Python3

 `# Python program for the above approach`   `# Function to find minimum` `# operations required to convert` `# N to 0` `def` `minimumOneBitOperations(n, res):` `  `  `  ``# Base case` `    ``if` `n ``=``=` `0``:` `        ``return` `res` `      `  `     ``# Store the highest power of 2` `    ``# less than or equal to n  ` `    ``b ``=` `1` `    ``while` `(b << ``1``) <``=` `n:` `        ``b ``=` `b << ``1` `        `  `    ``# Return the result` `    ``return` `minimumOneBitOperations((b >> ``1``) ^ b ^ n, res ``+` `b)`   `# Driver code` `N ``=` `6` `print``(minimumOneBitOperations(N, ``0``))`   `# This code is contributed by Parth Manchanda`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG{`   `// Function to find minimum` `// operations required to convert` `// N to 0` `static` `int` `minimumOneBitOperations(``int` `n, ``int` `res)` `{` `    ``// Base Case` `    ``if` `(n == 0)` `        ``return` `res;`   `    ``// Store the highest power of 2` `    ``// less than or equal to n` `    ``int` `b = 1;` `    ``while` `((b << 1) <= n)` `        ``b = b << 1;`   `    ``// Return the result` `    ``return` `minimumOneBitOperations(` `        ``(b >> 1) ^ b ^ n, res + b);` `}`   `// Driver Code` `public` `static` `void` `Main()` `{` `    ``// Given Input` `    ``int` `N = 6;`   `    ``// Function call` `    ``Console.Write(minimumOneBitOperations(N,0));` `}` `}`   `// This code is contributed by SURENDRA_GANGWAR.`

## Javascript

 ``

Output:

`4`

Time Complexity: O(log(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :