Find the n | Java program to find n - th palindrome ; utility function which is used to convert binary string into integer ; convert binary string into integer ; function to find nth binary palindrome number ; stores the binary palindrome string ; base case ; add 2 nd binary palindrome string ; runs till the nth binary palindrome number ; remove curr binary palindrome string from queue ; if n == 0 then we find the n 'th binary  palindrome so we return our answer ; calculate length of curr binary palindrome string ; if length is even . so it is our first case we have two choices ; if length is odd . so it is our second case we have only one choice ; Driver code ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static int convertStringToInt ( String s ) { int ans = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) ans += 1 << i ; } return ans ; } public static int getNthNumber ( int n ) { Queue < String > q = new LinkedList < > ( ) ; if ( n == 1 ) return 1 ; n = n - 1 ; q . add ( "11" ) ; while ( n -- > 0 ) { String curr = q . remove ( ) ; if ( n == 0 ) return convertStringToInt ( curr ) ; int len = curr . length ( ) ; if ( len % 2 == 0 ) { q . add ( curr . substring ( 0 , len / 2 ) + "0" + curr . substring ( len / 2 ) ) ; q . add ( curr . substring ( 0 , len / 2 ) + "1" + curr . substring ( len / 2 ) ) ; } else { char midChar = curr . charAt ( len / 2 ) ; q . add ( curr . substring ( 0 , len / 2 ) + midChar + curr . substring ( len / 2 ) ) ; } } return - 1 ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( getNthNumber ( n ) ) ; } }