Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | C ++ program to print subset at the nth position ordered by the sum of the elements ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printsubset ( int n , int k ) { int count = 0 , x = 0 ; vector < int > vec ; while ( n ) { x = n & 1 ; if ( x ) { vec . push_back ( pow ( k , count ) ) ; } n = n >> 1 ; count ++ ; } for ( int i = 0 ; i < vec . size ( ) ; i ++ ) cout << vec [ i ] << " ▁ " ; } int main ( ) { int n = 7 , k = 4 ; printsubset ( n , k ) ; return 0 ; }