Minimum number of given operations required to be performed to reduce N to 0
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++
#include <bits/stdc++.h>
using namespace std;
int minimumOneBitOperations( int n, int res = 0)
{
if (n == 0)
return res;
int b = 1;
while ((b << 1) <= n)
b = b << 1;
return minimumOneBitOperations(
(b >> 1) ^ b ^ n, res + b);
}
int main()
{
int N = 6;
cout << minimumOneBitOperations(N);
return 0;
}
|
Java
class GFG {
public static int minimumOneBitOperations( int n, int res)
{
if (n == 0 )
return res;
int b = 1 ;
while ((b << 1 ) <= n)
b = b << 1 ;
return minimumOneBitOperations((b >> 1 ) ^ b ^ n, res + b);
}
public static void main(String args[])
{
int N = 6 ;
System.out.println(minimumOneBitOperations(N, 0 ));
}
}
|
Python3
def minimumOneBitOperations(n, res):
if n = = 0 :
return res
b = 1
while (b << 1 ) < = n:
b = b << 1
return minimumOneBitOperations((b >> 1 ) ^ b ^ n, res + b)
N = 6
print (minimumOneBitOperations(N, 0 ))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int minimumOneBitOperations( int n, int res)
{
if (n == 0)
return res;
int b = 1;
while ((b << 1) <= n)
b = b << 1;
return minimumOneBitOperations(
(b >> 1) ^ b ^ n, res + b);
}
public static void Main()
{
int N = 6;
Console.Write(minimumOneBitOperations(N,0));
}
}
|
Javascript
<script>
function minimumOneBitOperations( n, res = 0)
{
if (n == 0)
return res;
let b = 1;
while ((b << 1) <= n)
b = b << 1;
return minimumOneBitOperations((b >> 1) ^ b ^ n, res + b);
}
let N = 6;
document.write(minimumOneBitOperations(N));
</script>
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...