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 ; Utility reverse function ; Driver function
using System ; using System . Text ; class GFG { static String obtain_str ( int n ) { int len = ( int ) Math . Floor ( ( Math . Log ( n + 1 ) / Math . Log ( 2 ) ) ) ; int rel_ind = n + 1 - ( int ) Math . Pow ( 2 , len ) ; int i = 0 ; StringBuilder str = new StringBuilder ( ) ; for ( i = 0 ; i < len ; i ++ ) { str . Append ( ' a ' ) ; } i = 0 ; while ( rel_ind > 0 ) { if ( rel_ind % 2 == 1 ) str [ i ] = ' b ' ; rel_ind /= 2 ; i ++ ; } return reverse ( str . ToString ( ) ) ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { int n = 11 ; Console . Write ( obtain_str ( n ) ) ; } }