Given two numbers N and K. The task is to print K numbers which are powers of 2 and their sum is N. Print -1 if not possible.
Examples:
Input: N = 9, K = 4
Output: 4 2 2 1
4 + 2 + 2 + 1 = 9
Input: N = 4, K = 5
Output: -1
Approach: The below algorithm can be followed to solve the above problem:
- If the K is less than the number of set bits in N or more than the number N, then it is not possible.
- Insert the powers of two at set bits into Priority Queue.
- Iterate in the Priority Queue till we get K elements, pop() the topmost element and
- push() element/2 twice into the Priority Queue again.
- Once K elements are achieved, print them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printNum( int n, int k)
{
int x = __builtin_popcount(n);
if (k < x || k > n) {
cout << "-1" ;
return ;
}
priority_queue< int > pq;
int two = 1;
while (n) {
if (n & 1) {
pq.push(two);
}
two = two * 2;
n = n >> 1;
}
while (pq.size() < k) {
int el = pq.top();
pq.pop();
pq.push(el / 2);
pq.push(el / 2);
}
int ind = 0;
while (ind < k) {
cout << pq.top() << " " ;
pq.pop();
ind++;
}
}
int main()
{
int n = 9, k = 4;
printNum(n, k);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void printNum( int n, int k)
{
String str = Integer.toBinaryString(n);
int x = 0 ;
for ( int i = 0 ; i < str.length(); i++)
if (str.charAt(i) == '1' )
x++;
if (k < x || k > n)
{
System.out.println( "-1" );
return ;
}
PriorityQueue<Integer> pq =
new PriorityQueue<>(Comparator.reverseOrder());
int two = 1 ;
while (n > 0 )
{
if ((n & 1 ) == 1 )
pq.add(two);
two *= 2 ;
n = n >> 1 ;
}
while (pq.size() < k)
{
int el = pq.poll();
pq.add(el / 2 );
pq.add(el / 2 );
}
int ind = 0 ;
while (ind < k)
{
System.out.print(pq.poll() + " " );
ind++;
}
}
public static void main(String[] args)
{
int n = 9 , k = 4 ;
printNum(n, k);
}
}
|
Python
def printNum(n, k):
x = 0
m = n
while (m):
x + = m & 1
m >> = 1
if k < x or k > n:
print ( "-1" )
return
pq = []
two = 1
while (n):
if (n & 1 ):
pq.append(two)
two = two * 2
n = n >> 1
while ( len (pq) < k):
el = pq[ - 1 ]
pq.pop()
pq.append(el / / 2 )
pq.append(el / / 2 )
ind = 0
pq.sort()
while (ind < k):
print (pq[ - 1 ], end = " " )
pq.pop()
ind + = 1
n = 9
k = 4
printNum(n, k)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void printNum( int n, int k)
{
int x = 0;
int m = n;
while (m > 0) {
x += m & 1;
m >>= 1;
}
if (k < x || k > n) {
Console.WriteLine( "-1" );
return ;
}
List< int > pq = new List< int >();
int two = 1;
while (n > 0) {
if ((n & 1) != 0)
pq.Add(two);
two = two * 2;
n = n >> 1;
}
while (pq.Count < k) {
int el = pq[pq.Count - 1];
pq.RemoveAt(pq.Count - 1);
pq.Add(el / 2);
pq.Add(el / 2);
}
int ind = 0;
pq.Sort();
while (ind < k) {
Console.Write(pq[pq.Count - 1] + " " );
pq.RemoveAt(pq.Count - 1);
ind += 1;
}
}
public static void Main( string [] args)
{
int n = 9;
int k = 4;
printNum(n, k);
}
}
|
Javascript
<script>
function printNum(n, k)
{
let x = 0
let m = n
while (m){
x += m & 1
m >>= 1
}
if (k < x || k > n){
document.write( "-1" , "</br>" )
return
}
let pq = []
let two = 1
while (n){
if (n & 1)
pq.push(two)
two = two * 2
n = n >> 1
}
while (pq.length < k){
let el = pq[pq.length -1]
pq.pop()
pq.push(Math.floor(el / 2))
pq.push(Math.floor(el / 2))
}
let ind = 0
pq.sort()
while (ind < k){
document.write(pq[pq.length -1], " " )
pq.pop()
ind += 1
}
}
let n = 9
let k = 4
printNum(n, k)
</script>
|
Time Complexity: O(N*logN), as we are using a loop to traverse N times and in each traversal we are using priority queue operation which will cost logN time.
Auxiliary Space: O(N), as we are using extra space for the priority queue.
Represent n as the sum of exactly k powers of two | Set 2