Find the n | C ++ implementation of the above approach ; Function to return the nth string in the required sequence ; Length of the resultant string ; Relative index ; Initial string of length len consists of all a 's since the list is sorted ; Convert relative index to Binary form and set 0 = a and 1 = b ; Reverse and return the string ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE string obtain_str ( ll n ) { ll len = ( int ) log2 ( n + 1 ) ; ll rel_ind = n + 1 - pow ( 2 , len ) ; ll i = 0 ; string str = " " ; for ( i = 0 ; i < len ; i ++ ) { str += ' a ' ; } i = 0 ; while ( rel_ind > 0 ) { if ( rel_ind % 2 == 1 ) str [ i ] = ' b ' ; rel_ind /= 2 ; i ++ ; } reverse ( str . begin ( ) , str . end ( ) ) ; return str ; } int main ( ) { ll n = 11 ; cout << obtain_str ( n ) ; return 0 ; }