Find the n | Java 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
import java . io . * ; import java . util . * ; 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 . setCharAt ( i , ' b ' ) ; rel_ind /= 2 ; i ++ ; } str = str . reverse ( ) ; return str . toString ( ) ; } public static void main ( String args [ ] ) { int n = 11 ; System . out . print ( obtain_str ( n ) ) ; } }