Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | Function to print the elements of the subset at pos n ; Initialize count = 0 and x = 0 ; Create a vector for storing the elements of subsets ; Doing until all the set bits of n are used ; This part is executed only when the last bit is set ; Right shift the bit by one position ; Increasing the count each time by one ; Printing the values os elements ; Driver code
function printsubset ( n , k ) { let count = 0 , x = 0 ; let vec = [ ] ; while ( n != 0 ) { x = n & 1 ; if ( x != 0 ) { vec . push ( Math . pow ( k , count ) ) ; } n = n >> 1 ; count ++ ; } for ( let i = 0 ; i < vec . length ; i ++ ) document . write ( vec [ i ] + " " ) ; } let n = 7 , k = 4 ; printsubset ( n , k ) ;