Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum | C # program for the above approach ; Function to print the required N - th subset ; Nearest power of 2 <= N ; Now insert k ^ p in the answer ; update n ; Print the ans in sorted order ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printSubset ( int n , int k ) { List < int > answer = new List < int > ( ) ; while ( n > 0 ) { int p = ( int ) Math . Log ( n , 2 ) ; answer . Add ( ( int ) Math . Pow ( k , p ) ) ; n %= ( int ) Math . Pow ( 2 , p ) ; } answer . Reverse ( ) ; foreach ( int x in answer ) { Console . Write ( x + " ▁ " ) ; } } static void Main ( ) { int n = 5 ; int k = 4 ; printSubset ( n , k ) ; } }