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 = 4Output:4 2 2 1 4 + 2 + 2 + 1 = 9Input:N = 4, K = 5Output:-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()
- Once K elements are achieved, print them.

element/2 twice into the Priority Queue again.

Below is the implementation of the above approach:

## C++

`// CPP program to find k numbers that ` `// are power of 2 and have sum equal ` `// to N ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to print numbers ` `void` `printNum(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Count the number of set bits ` ` ` `int` `x = __builtin_popcount(n); ` ` ` ` ` `// Not-possible condition ` ` ` `if` `(k < x || k > n) { ` ` ` `cout << ` `"-1"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Stores the number ` ` ` `priority_queue<` `int` `> pq; ` ` ` ` ` `// Get the set bits ` ` ` `int` `two = 1; ` ` ` `while` `(n) { ` ` ` `if` `(n & 1) { ` ` ` `pq.push(two); ` ` ` `} ` ` ` ` ` `two = two * 2; ` ` ` `n = n >> 1; ` ` ` `} ` ` ` ` ` `// Iterate till we get K elements ` ` ` `while` `(pq.size() < k) { ` ` ` ` ` `// Get the topmost element ` ` ` `int` `el = pq.top(); ` ` ` `pq.pop(); ` ` ` ` ` `// Push the elements/2 into ` ` ` `// priority queue ` ` ` `pq.push(el / 2); ` ` ` `pq.push(el / 2); ` ` ` `} ` ` ` ` ` `// Print all elements ` ` ` `int` `ind = 0; ` ` ` `while` `(ind < k) { ` ` ` `cout << pq.top() << ` `" "` `; ` ` ` `pq.pop(); ` ` ` `ind++; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 9, k = 4; ` ` ` `printNum(n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find k numbers that ` `// are power of 2 and have sum equal ` `// to N ` `import` `java.io.*; ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// function to print numbers ` ` ` `static` `void` `printNum(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` ` ` `// Count the number of set bits ` ` ` `String str = Integer.toBinaryString(n); ` ` ` `int` `x = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < str.length(); i++) ` ` ` `if` `(str.charAt(i) == ` `'1'` `) ` ` ` `x++; ` ` ` ` ` `// Not-possible condition ` ` ` `if` `(k < x || k > n) ` ` ` `{ ` ` ` `System.out.println(` `"-1"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Stores the number ` ` ` `PriorityQueue<Integer> pq = ` ` ` `new` `PriorityQueue<>(Comparator.reverseOrder()); ` ` ` ` ` `// Get the set bits ` ` ` `int` `two = ` `1` `; ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `if` `((n & ` `1` `) == ` `1` `) ` ` ` `pq.add(two); ` ` ` `two *= ` `2` `; ` ` ` `n = n >> ` `1` `; ` ` ` `} ` ` ` ` ` `// Iterate till we get K elements ` ` ` `while` `(pq.size() < k) ` ` ` `{ ` ` ` ` ` `// Get the topmost element ` ` ` `int` `el = pq.poll(); ` ` ` ` ` `// Push the elements/2 into ` ` ` `// priority queue ` ` ` `pq.add(el / ` `2` `); ` ` ` `pq.add(el / ` `2` `); ` ` ` `} ` ` ` ` ` `// Print all elements ` ` ` `int` `ind = ` `0` `; ` ` ` `while` `(ind < k) ` ` ` `{ ` ` ` `System.out.print(pq.poll() + ` `" "` `); ` ` ` `ind++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `9` `, k = ` `4` `; ` ` ` `printNum(n, k); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find k numbers that ` `# are power of 2 and have sum equal ` `# to N ` ` ` `# function to prnumbers ` `def` `printNum(n, k): ` ` ` ` ` `# Count the number of set bits ` ` ` `x ` `=` `0` ` ` `m ` `=` `n ` ` ` `while` `(m): ` ` ` `x ` `+` `=` `m & ` `1` ` ` `m >>` `=` `1` ` ` ` ` `# Not-possible condition ` ` ` `if` `k < x ` `or` `k > n: ` ` ` `print` `(` `"-1"` `) ` ` ` `return` ` ` ` ` `# Stores the number ` ` ` `pq ` `=` `[] ` ` ` ` ` `# Get the set bits ` ` ` `two ` `=` `1` ` ` `while` `(n): ` ` ` `if` `(n & ` `1` `): ` ` ` `pq.append(two) ` ` ` ` ` `two ` `=` `two ` `*` `2` ` ` `n ` `=` `n >> ` `1` ` ` ` ` `# Iterate till we get K elements ` ` ` `while` `(` `len` `(pq) < k): ` ` ` ` ` `# Get the topmost element ` ` ` `el ` `=` `pq[` `-` `1` `] ` ` ` `pq.pop() ` ` ` ` ` `# append the elements/2 into ` ` ` `# priority queue ` ` ` `pq.append(el ` `/` `/` `2` `) ` ` ` `pq.append(el ` `/` `/` `2` `) ` ` ` ` ` `# Prall elements ` ` ` `ind ` `=` `0` ` ` `pq.sort() ` ` ` `while` `(ind < k): ` ` ` `print` `(pq[` `-` `1` `], end ` `=` `" "` `) ` ` ` `pq.pop() ` ` ` `ind ` `+` `=` `1` ` ` `# Driver Code ` `n ` `=` `9` `k ` `=` `4` `printNum(n, k) ` ` ` `# This code is contributed by SHUBHAMSINGH10 ` |

*chevron_right*

*filter_none*

**Output:**

4 2 2 1

Represent n as the sum of exactly k powers of two | Set 2

## Recommended Posts:

- Find the sum of numbers from 1 to n excluding those which are powers of K
- Sum of first N natural numbers which are not powers of K
- Count of numbers in range which are divisible by M and have digit D at odd places
- Sum of fourth powers of the first n natural numbers
- Sum of fifth powers of the first n natural numbers
- Sum of fourth powers of first n odd natural numbers
- Sum of first N natural numbers by taking powers of 2 as negative number
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Sum of first N natural numbers with all powers of 2 added twice
- Print all integers that are sum of powers of two given numbers
- Number of triangles possible with given lengths of sticks which are powers of 2
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Count the numbers < N which have equal number of divisors as K
- Represent n as the sum of exactly k powers of two | Set 2
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Find the count of M character words which have at least one character repeated
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Sum of largest divisible powers of p (a prime number) in a range
- Powers of 2 to required sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.