human
What is a code to find k numbers which are powers of 2 and have sum N and the other steps required given the following:
Find k numbers which are powers of 2 and have sum N | Set 1 | function to prnumbers.
Count the number of set bits.
Not - possible condition.
Stores the number.
Get the set bits.
Iterate till we get K elements.
Get the topmost element.
append the elements / 2 into priority queue.
Prall elements.
Driver Code"
Solution in Python:
assistant
#Here is code to find k numbers which are powers of 2 and have sum N and the other steps required.
 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 )