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 else print Impossible.
Examples:
Input: n = 9, k = 4
Output: 1 2 2 4
1, 2 and 4 are all powers of 2 and 1 + 2 + 2 + 4 = 9.
Input: n = 3, k = 7
Output: Impossible
It is impossible since 3 cannot be represented as sum of 7 numbers which are powers of 2.
We have discussed one approach to solve this problem in Find k numbers which are powers of 2 and have sum N. In this post, a different approach is being discussed.
Approach:
- Create an array arr[] of size k with all elements initialized to 1 and create a variable sum = k.
- Now starting from the last element of arr[]
- If sum + arr[i] ? n then update sum = sum + arr[i] and arr[i] = arr[i] * 2.
- Else skip the current element.
- If sum = n then the contents of arr[] are the required elements.
- Else it is impossible to represent n as exactly k powers of 2.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void FindAllElements( int n, int k)
{
int sum = k;
int A[k];
fill(A, A + k, 1);
for ( int i = k - 1; i >= 0; --i) {
while (sum + A[i] <= n) {
sum += A[i];
A[i] *= 2;
}
}
if (sum != n) {
cout << "Impossible" ;
}
else {
for ( int i = 0; i < k; ++i)
cout << A[i] << ' ' ;
}
}
int main()
{
int n = 12;
int k = 6;
FindAllElements(n, k);
return 0;
}
|
Java
import java.util.Arrays;
public class GfG {
public static void FindAllElements( int n, int k)
{
int sum = k;
int [] A = new int [k];
Arrays.fill(A, 0 , k, 1 );
for ( int i = k - 1 ; i >= 0 ; --i) {
while (sum + A[i] <= n) {
sum += A[i];
A[i] *= 2 ;
}
}
if (sum != n) {
System.out.print( "Impossible" );
}
else {
for ( int i = 0 ; i < k; ++i)
System.out.print(A[i] + " " );
}
}
public static void main(String []args){
int n = 12 ;
int k = 6 ;
FindAllElements(n, k);
}
}
|
Python3
def FindAllElements(n, k):
sum = k
A = [ 1 for i in range (k)]
i = k - 1
while (i > = 0 ):
while ( sum + A[i] < = n):
sum + = A[i]
A[i] * = 2
i - = 1
if ( sum ! = n):
print ( "Impossible" )
else :
for i in range ( 0 , k, 1 ):
print (A[i], end = ' ' )
if __name__ = = '__main__' :
n = 12
k = 6
FindAllElements(n, k)
|
C#
using System;
class GfG
{
public static void FindAllElements( int n, int k)
{
int sum = k;
int [] A = new int [k];
for ( int i = 0; i < k; i++)
A[i] = 1;
for ( int i = k - 1; i >= 0; --i)
{
while (sum + A[i] <= n)
{
sum += A[i];
A[i] *= 2;
}
}
if (sum != n)
{
Console.Write( "Impossible" );
}
else
{
for ( int i = 0; i < k; ++i)
Console.Write(A[i] + " " );
}
}
public static void Main(String []args)
{
int n = 12;
int k = 6;
FindAllElements(n, k);
}
}
|
PHP
<?php
function FindAllElements( $n , $k )
{
$sum = $k ;
$A = array_fill (0, $k , 1) ;
for ( $i = $k - 1; $i >= 0; -- $i )
{
while ( $sum + $A [ $i ] <= $n )
{
$sum += $A [ $i ];
$A [ $i ] *= 2;
}
}
if ( $sum != $n )
{
echo "Impossible" ;
}
else
{
for ( $i = 0; $i < $k ; ++ $i )
echo $A [ $i ], ' ' ;
}
}
$n = 12;
$k = 6;
FindAllElements( $n , $k );
?>
|
Javascript
<script>
function FindAllElements( n, k)
{
let sum = k;
let A = new Array(k).fill(1);
for (let i = k - 1; i >= 0; --i) {
while (sum + A[i] <= n) {
sum += A[i];
A[i] *= 2;
}
}
if (sum != n) {
document.write( "Impossible" );
}
else {
for (let i = 0; i < k; ++i)
document.write(A[i] + " " );
}
}
let n = 12;
let k = 6;
FindAllElements(n, k);
</script>
|
Complexity Analysis:
- Time Complexity: O(k*log2(n-k))
- Auxiliary Space: O(k), since k extra space has been taken.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Sep, 2022
Like Article
Save Article