Represent N as the sum of exactly K powers of two | Set 3
Given two integers N and K, the task is to find whether it is possible to represent N as the sum of exactly K powers of 2. If possible, then print K positive integers such that they are powers of 2 and their sum is exactly equal to N. Otherwise, print “Impossible”. If multiple answers exist, print any.
Examples:
Input: N = 5, K = 2
Output: 4 1
Explanation:
The only way of representing N as K numbers that are powers of 2 is {4, 1}.
Input: N = 7, K = 4
Output: 4 1 1 1
Explanation:
The possible ways of representing N as K numbers that are powers of 2 are {4, 1, 1, 1} and {2, 2, 2, 1}.
Priority Queue based Approach: Refer to this article to solve the problem using Priority Queue.
Recursive Approach: Refer to this article to solve the problem using Recursion.
Alternate Approach: The idea is to use the Greedy Approach to solve this problem. Below are the steps:
- Initialize an integer, say num = 31, and a vector of integers, say res, to store the K numbers which are powers of 2.
- Check if the number of bits in N is greater than K or if N is less than K, then print “Impossible”.
- Iterate while num ? 0 and K > 0:
- Check if N – 2num is less than K – 1. If found to be true, then decrement num by one and continue.
- Otherwise, decrease K by one, and N by 2num and push num into the vector res.
- Finally, print the vector res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void nAsKPowersOfTwo( int N, int K)
{
int x = __builtin_popcount(N);
if (K < x || K > N) {
cout << "Impossible" ;
return ;
}
int num = 31;
vector< int > res;
while (num >= 0 && K) {
int val = pow (2, num);
if (N - val < K - 1) {
--num;
continue ;
}
--K;
N -= val;
res.push_back(num);
}
for ( auto x : res)
cout << pow (2, x) << " " ;
}
int main()
{
int N = 7, K = 4;
nAsKPowersOfTwo(N, K);
}
|
Java
import java.util.*;
class GFG{
static void nAsKPowersOfTwo( int N, int K)
{
int x = Integer.bitCount(N);
if (K < x || K > N)
{
System.out.print( "Impossible" );
return ;
}
int num = 31 ;
Vector<Integer> res = new Vector<Integer>();
while (num >= 0 && K > 0 )
{
int val = ( int ) Math.pow( 2 , num);
if (N - val < K - 1 )
{
--num;
continue ;
}
--K;
N -= val;
res.add(num);
}
for ( int i : res)
System.out.print(( int )Math.pow( 2 , i)+ " " );
}
public static void main(String[] args)
{
int N = 7 , K = 4 ;
nAsKPowersOfTwo(N, K);
}
}
|
Python3
def nAsKPowersOfTwo(N, K):
x = bin (N).count( '1' )
if (K < x or K > N):
cout << "Impossible"
return
num = 31
res = []
while (num > = 0 and K):
val = pow ( 2 , num)
if (N - val < K - 1 ):
num - = 1
continue
K - = 1
N - = val
res.append(num)
for x in res:
print ( pow ( 2 , x), end = " " )
if __name__ = = '__main__' :
N, K = 7 , 4
nAsKPowersOfTwo(N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void nAsKPowersOfTwo( int N, int K)
{
int x = countSetBits(N);
if (K < x || K > N)
{
Console.Write( "Impossible" );
return ;
}
int num = 31;
List< int > res = new List< int >();
while (num >= 0 && K > 0)
{
int val = ( int ) Math.Pow(2, num);
if (N - val < K - 1)
{
--num;
continue ;
}
--K;
N -= val;
res.Add(num);
}
foreach ( int i in res)
Console.Write(( int )Math.Pow(2, i)+ " " );
}
static int countSetBits( long x)
{
int setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
public static void Main(String[] args)
{
int N = 7, K = 4;
nAsKPowersOfTwo(N, K);
}
}
|
Javascript
<script>
function nAsKPowersOfTwo(N, K)
{
var x = countSetBits(N);
if (K < x || K > N)
{
document.write( "Impossible" );
return ;
}
var num = 31;
var res=[];
while (num >= 0 && K > 0)
{
var val = parseInt( Math.pow(2, num));
if (N - val < K - 1)
{
--num;
continue ;
}
--K;
N -= val;
res.push(num);
}
for ( var i = 0;i<res.length;i++){
document.write(parseInt(Math.pow(2, res[i]))+ " " );
}
}
function countSetBits(x)
{
var setBits = 0;
while (x != 0)
{
x = x & (x - 1);
setBits++;
}
return setBits;
}
var N = 7, K = 4;
nAsKPowersOfTwo(N, K);
</script>
|
Time Complexity: O(32)
Auxiliary Space: O(1)
Last Updated :
23 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...