Minimize subtraction of power of 2 to convert N to 0
Last Updated :
24 Mar, 2023
Given a positive integer N, the task is to find the minimum number of subtractions of power of 2 required to convert N to 0.
Examples:
Input: 10
Output: 2
Explanation: When we subtract 8 from 10 ( 10 – (2^3) = 2) then 2 will remain.
After that subtract 2 from 2^0 i.e., 2 – 2^0 = 0.
Hence we are doing two operation to make the N = 0.
Input: 5
Output: 2
Approach: The approach of the problem is based on the following idea:
As we need to minimize the number of subtractions then subtract as big a a power of 2 as possible. This will be same as the number of set bits in the binary representation of N.
Follow the below illustration for a better understanding.
Illustration:
Take N = 10
1st step: The maximum value that can be subtracted is 8
So N = 10 – 8 = 2.
2nd step: The maximum value that can be subtracted is 2
So N = 2 – 2 = 0.
Now see the binary representation of 10 = “1010”.
It has 2 set bits. So minimum steps required = 2
Follow the below step to implement the approach:
- Iterate from i = 1 to 63:
- Check if that bit of the binary representation of N is set.
- If set, increment the count of set bits.
- Return the final count of set bits.
Below is the implementation for the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int find_no_of_set_bits( long long n)
{
int set_bit_count = 0;
for ( int i = 0; i < 63; i++) {
if (n & (1LL << i)) {
set_bit_count++;
}
}
return set_bit_count;
}
int main()
{
long long N = 10;
cout << find_no_of_set_bits(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int find_no_of_set_bits( long n)
{
int set_bit_count = 0 ;
for ( int i = 0 ; i < 63 ; i++) {
if ((n & (( long ) 1 << i)) != 0 ) {
set_bit_count++;
}
}
return set_bit_count;
}
public static void main(String[] args)
{
long N = 10 ;
System.out.println(find_no_of_set_bits(N));
}
}
|
Python3
def find_no_of_set_bits(n):
set_bit_count = 0
for i in range ( 63 ):
if (n & ( 1 << i)):
set_bit_count + = 1
return set_bit_count
N = 10
print (find_no_of_set_bits(N))
|
C#
using System;
public class GFG {
public static int find_no_of_set_bits( long n)
{
int set_bit_count = 0;
for ( int i = 0; i < 63; i++) {
if ((n & (( long )1 << i)) != 0) {
set_bit_count++;
}
}
return set_bit_count;
}
public static void Main( string [] args)
{
long N = 10;
Console.WriteLine(find_no_of_set_bits(N));
}
}
|
Javascript
<script>
function find_no_of_set_bits(n)
{
var set_bit_count = 0;
for ( var i = 0; i < 32; i++) {
if ((n & (1 << i)) != 0) {
set_bit_count++;
}
}
return set_bit_count;
}
var N = 10;
document.write(find_no_of_set_bits(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Another Approach
Convert the number to its binary string format using in – built functions, then count the numbers of “1” present in the binary string using in – built functions as well.
C++
#include <bits/stdc++.h>
using namespace std;
int find_no_of_set_bits( int n)
{
int no_of_set_bits = 0;
for ( int i = 1; i <= n; i++) {
string str = to_string(i);
no_of_set_bits += count(str.begin(), str.end(), '1' );
}
return no_of_set_bits;
}
int main()
{
int N = 10;
cout << find_no_of_set_bits(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int find_no_of_set_bits( int n)
{
int no_of_set_bits = 0 ;
for ( int i = 1 ; i <= n; i++) {
String bin_N = String.valueOf(i);
no_of_set_bits += bin_N.split( "1" , - 1 ).length - 1 ;
}
return no_of_set_bits;
}
public static void main(String[] args)
{
int N = 10 ;
System.out.println(find_no_of_set_bits(N));
}
}
|
Python3
def find_no_of_set_bits(n):
bin_N = bin (N)
no_of_set_bits = bin_N.count( "1" )
return no_of_set_bits
N = 10
print (find_no_of_set_bits(N))
|
C#
using System;
class GFG {
static int find_no_of_set_bits( int n)
{
int no_of_set_bits = 0;
for ( int i = 1; i <= n; i++) {
string bin_N = i.ToString();
no_of_set_bits += bin_N.Split( '1' ).Length - 1;
}
return no_of_set_bits;
}
public static void Main(String[] args)
{
int N = 10;
Console.Write(find_no_of_set_bits(N));
}
}
|
Javascript
<script>
function find_no_of_set_bits(n)
{
var bin_N = N.toString(2);
var no_of_set_bits = (bin_N.match(/1/g) || []).length;
return no_of_set_bits;
}
var N = 10;
document.write(find_no_of_set_bits(N))
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Another Approach
The number of set bits in a number can be counted in O(1) time using a lookup table. The implementation of the lookup table is shown below:
C++
#include <bits/stdc++.h>
using namespace std;
const int LIMIT = 64;
int lookUpTable[LIMIT];
void createLookUpTable()
{
lookUpTable[0] = 0;
for ( int i = 0; i < LIMIT; i++) {
lookUpTable[i] = (i & 1) + lookUpTable[i / 2];
}
}
int countSetBits( int n)
{
return (lookUpTable[n & 0xff]
+ lookUpTable[(n >> 8) & 0xff]
+ lookUpTable[(n >> 16) & 0xff]
+ lookUpTable[n >> 24]);
}
int main()
{
createLookUpTable();
int n = 10;
cout << countSetBits(n);
}
|
Java
import java.io.*;
class GFG {
static final int LIMIT = 64 ;
static int [] lookUpTable = new int [LIMIT];
public static void createLookUpTable()
{
lookUpTable[ 0 ] = 0 ;
for ( int i = 0 ; i < LIMIT; i++) {
lookUpTable[i] = (i & 1 ) + lookUpTable[i / 2 ];
}
}
public static int countSetBits( int n)
{
return (lookUpTable[n & 0xff ]
+ lookUpTable[(n >> 8 ) & 0xff ]
+ lookUpTable[(n >> 16 ) & 0xff ]
+ lookUpTable[n >> 24 ]);
}
public static void main(String[] args)
{
createLookUpTable();
int n = 10 ;
System.out.println(countSetBits(n));
}
}
|
Python3
LIMIT = 64
lookUpTable = [ None for _ in range (LIMIT)]
def createLookUpTable():
lookUpTable[ 0 ] = 0
for i in range (LIMIT):
lookUpTable[i] = (i & 1 ) + lookUpTable[(i / / 2 )]
def countSetBits(n):
return (lookUpTable[n & 0xff ] + lookUpTable[(n >> 8 ) & 0xff ] + lookUpTable[(n >> 16 ) & 0xff ] + lookUpTable[n >> 24 ])
createLookUpTable()
n = 10
print (countSetBits(n))
|
C#
using System;
class GFG {
static int LIMIT = 64;
static int [] lookUpTable = new int [LIMIT];
public static void createLookUpTable()
{
lookUpTable[0] = 0;
for ( int i = 0; i < LIMIT; i++) {
lookUpTable[i] = (i & 1) + lookUpTable[i / 2];
}
}
public static int countSetBits( int n)
{
return (lookUpTable[n & 0xff]
+ lookUpTable[(n >> 8) & 0xff]
+ lookUpTable[(n >> 16) & 0xff]
+ lookUpTable[n >> 24]);
}
public static void Main( string [] args)
{
createLookUpTable();
int n = 10;
Console.WriteLine(countSetBits(n));
}
}
|
Javascript
let LIMIT = 64;
let lookUpTable = new Array(LIMIT);
function createLookUpTable()
{
lookUpTable[0] = 0;
for (let i = 0; i < LIMIT; i++) {
lookUpTable[i]
= (i & 1) + lookUpTable[(Math.floor(i / 2))];
}
}
function countSetBits(n)
{
return (lookUpTable[n & 0xff]
+ lookUpTable[(n >> 8) & 0xff]
+ lookUpTable[(n >> 16) & 0xff]
+ lookUpTable[n >> 24]);
}
createLookUpTable();
let n = 10;
console.log(countSetBits(n));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...