Open In App

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

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:

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:

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:

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
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 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.




# 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# 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.




<script>
    // Javascript program for the above approach
 
   
// Function to find minimum
// operations required to convert
// N to 0
function minimumOneBitOperations( n,  res = 0)
{
    // Base Case
    if (n == 0)
        return res;
   
    // Store the highest power of 2
    // less than or equal to n
    let b = 1;
    while ((b << 1) <= n)
        b = b << 1;
   
    // Return the result
    return minimumOneBitOperations((b >> 1) ^ b ^ n, res + b);
}
   
// Driver Code
 
    // Given Input
    let N = 6;
   
    // Function call
    document.write(minimumOneBitOperations(N));
    
// This code is contributed by
// Potta Lokesh
     
    </script>

 
 

Output: 
4

 

 

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

 


Article Tags :