Find the n | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int convertStringToInt ( string s ) { int num = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { num = num * 2 + ( s [ i ] - '0' ) ; } return num ; } int getNthNumber ( int n ) { queue < string > q ; if ( n == 1 ) return 1 ; n -- ; q . push ( "11" ) ; while ( ! q . empty ( ) ) { string curr = q . front ( ) ; q . pop ( ) ; n -- ; if ( ! n ) { return convertStringToInt ( curr ) ; } int mid = curr . size ( ) / 2 ; if ( curr . size ( ) % 2 == 0 ) { string s0 = curr , s1 = curr ; s0 . insert ( mid , "0" ) ; s1 . insert ( mid , "1" ) ; q . push ( s0 ) ; q . push ( s1 ) ; } else { string ch ( 1 , curr [ mid ] ) ; string temp = curr ; temp . insert ( mid , ch ) ; q . push ( temp ) ; } } return 0 ; } int main ( ) { int n = 9 ; cout << getNthNumber ( n ) ; }