Find Nth even length palindromic number formed using digits X and Y | Java program to find nth even palindromic number of only even length composing of 4 ' s ▁ and ▁ 5' s . ; Utility function to compute n 'th palindrome number  ; Calculate the length from above formula as discussed above ; Calculate rank for length L ; Mask to check if i 't bit   is set or not  ; If bit is set append '5' else append '4' ; Driver Code
import java . util . * ; class GFG { static String solve ( int n , char x , char y ) { int length = ( int ) Math . ceil ( Math . log ( n + 2 ) / Math . log ( 2 ) ) - 1 ; int rank = n - ( 1 << length ) + 1 ; String left = " " , right = " " ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int mask = ( 1 << i ) ; int bit = mask & rank ; if ( bit > 0 ) { left += y ; right += y ; } else { left += x ; right += x ; } } StringBuilder sb = new StringBuilder ( right ) ; sb . reverse ( ) ; right = sb . toString ( ) ; String res = left + right ; return res ; } public static void main ( String [ ] args ) { int n = 23 ; char x = '4' , y = '5' ; String ans = solve ( n , x , y ) ; System . out . println ( ans ) ; } }