Find Nth even length palindromic number formed using digits X and Y

Given an integer N, the task is to find the Nth even palindromic number of even length and only comprising of the digits X and Y where X, Y > 0.
Examples:
Input: N = 9, X = 4, Y = 5
Output: 454454
Explanation:
Even length palindromic numbers using 4 & 5 are
44, 55, 4444, 4554, 5445, 5555, 444444, 445544, 454454, …
9th term of the above series = 454454
Input: N = 6, X = 1, Y = 2
Output: 2222
Explanation:
Even length palindromic numbers using 1 & 2 are
11, 22, 1111, 1221, 2112, 2222, 111111, 112211, 121121, …
6th term of the above series = 2222
Approach:
- Even length palindromic numbers using X & Y are
XX, YY, XXXX, XYYX, YXXY, YYYY, XXXXXX, XXYYXX, ...
- The above sequence can be observed as:
XX, -> Length (L) = 2 YY, -> Length (L) = 2 XXXX, -> Length (L) = 4 XYYX, -> Length (L) = 4 YXXY, -> Length (L) = 4 YYYY, -> Length (L) = 4 XXXXXX, -> Length (L) = 6 XXYYXX, -> Length (L) = 6 XYXXYX, -> Length (L) = 6 XYYYYX, -> Length (L) = 6 YXXXXY, -> Length (L) = 6 YXYYXY, -> Length (L) = 6 YYXXYY, -> Length (L) = 6 YYYYYY, -> Length (L) = 6 XXXXXXXX, -> Length (L) = 8 ...
- If we divide any term into 2 halves, the second half is just the reverse of the first half
Example:
Taking the term XXYYXX Dividing this into 2 halves XXYYXX = XXY | YXX So YXX is just the reverse of XXY
- Taking the left half only of the terms and putting X = 0 and Y = 1 to get the Binary String, the numbers of length L can be seen forming a integer sequence from 0 to (2L/2 – 1), taken as Rank (R). Therefore 0 ≤ R ≤ 2L/2 – 1
Therefore the sequence can be observed as follows:
L -> Left Half -> Binary String -> Rank (in Decimal) 2 -> X -> 0 -> 0 2 -> Y -> 1 -> 1 4 -> XX -> 00 -> 0 4 -> XY -> 01 -> 1 4 -> YX -> 10 -> 2 4 -> YY -> 11 -> 3 6 -> XXX -> 000 -> 0 6 -> XXY -> 001 -> 1 6 -> XYX -> 010 -> 2 6 -> XYY -> 011 -> 3 6 -> YXX -> 100 -> 4 6 -> YXY -> 101 -> 5 6 -> YYX -> 110 -> 6 6 -> YYY -> 111 -> 7 8 -> XXXX -> 0000 -> 0 ...
- Therefore, For the required term N:
- The length (L) of the required Nth term:
- Rank (R) of the required Nth term:
- First Half of the required Nth term = Binary representation of R in L/2 bits by replacing 0 as X and 1 as Y
- Second Half of the required Nth term = Reverse of the First Half
Below is the implementation of the above approach:
C++
// C++ program to find nth even// palindromic number of only even// length composing of 4's and 5's.#include <bits/stdc++.h>usingnamespacestd;// Utility function to compute// n'th palindrome numberstring solve(intn,charx,chary){// Calculate the length from above// formula as discussed aboveintlength =ceil(log2(n + 2)) - 1;// Calculate rank for length Lintrank = n - (1 << length) + 1;string left ="", right ="";for(inti = length - 1; i >= 0; i--) {// Mask to check if i't bit// is set or notintmask = 1 << i;// If bit is set append '5' else append '4'boolbit = mask & rank;if(bit) {left += y;right += y;}else{left += x;right += x;}}reverse(right.begin(), right.end());returnleft + right;}// Driver Codeintmain(){intn = 23;charx ='4', y ='5';string ans = solve(n, x, y);cout << ans <<'\n';return0;}Java
// Java program to find nth even// palindromic number of only even// length composing of 4's and 5's.importjava.util.*;classGFG{// Utility function to compute// n'th palindrome numberstaticString solve(intn,charx,chary){// Calculate the length from above// formula as discussed aboveintlength = (int)Math.ceil(Math.log(n +2) /Math.log(2)) -1;// Calculate rank for length Lintrank = n - (1<< length) +1;String left ="", right ="";for(inti = length -1; i >=0; i--){// Mask to check if i't bit// is set or notintmask = (1<< i);// If bit is set append '5' else append '4'intbit = mask & rank;if(bit >0){left += y;right += y;}else{left += x;right += x;}}StringBuilder sb =newStringBuilder(right);sb.reverse();right = sb.toString();String res = left + right;returnres;}// Driver Codepublicstaticvoidmain (String[] args){intn =23;charx ='4', y ='5';String ans = solve(n, x, y);System.out.println(ans);}}// This code is contributed by AnkitRai01Python3
# Python3 program to find nth even# palindromic number of only even# length composing of 4's and 5's.frommathimportceil, log2# Utility function to compute# n'th palindrome numberdefsolve(n, x, y) :# Calculate the length from above# formula as discussed abovelength=ceil(log2(n+2))-1;# Calculate rank for length Lrank=n-(1<< length)+1;left=""; right = "";foriinrange(length-1,-1,-1):# Mask to check if i't bit# is set or notmask=(1<< i);# If bit is set append '5'# else append '4'bit=(mask & rank);if(bit) :left+=y;right+=y;else:left+=x;right+=x;right=right[::-1];res=left+right;returnres;# Driver Codeif__name__=="__main__":n=23;x='4';y='5';ans=solve(n, x, y);print(ans);# This code is contributed by kanugargngC#
// C# program to find nth even// palindromic number of only even// length composing of 4's and 5's.usingSystem;classGFG{// Utility function to compute// n'th palindrome numberstaticString solve(intn,charx,chary){// Calculate the length from above// formula as discussed aboveintlength = (int)Math.Ceiling(Math.Log(n + 2) /Math.Log(2)) - 1;// Calculate rank for length Lintrank = n - (1 << length) + 1;String left ="", right ="";for(inti = length -1; i >= 0; i--){// Mask to check if i't bit// is set or notintmask = (1 << i);// If bit is set append '5'// else append '4'intbit = mask & rank;if(bit > 0){left += y;right += y;}else{left += x;right += x;}}right = reverse(right);String res = left + right;returnres;}staticString reverse(String input){char[] a = input.ToCharArray();intl, r = 0;r = a.Length - 1;for(l = 0; l < r; l++, r--){// Swap values of l and rchartemp = a[l];a[l] = a[r];a[r] = temp;}returnString.Join("", a);}// Driver CodepublicstaticvoidMain (String[] args){intn = 23;charx ='4', y ='5';String ans = solve(n, x, y);Console.WriteLine(ans);}}// This code is contributed by Rajput-JiJavascript
<script>// Javascript 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 numberfunctionsolve(n, x, y){// Calculate the length from above// formula as discussed abovevarlength = Math.ceil(Math.log2(n + 2)) - 1;// Calculate rank for length Lvarrank = n - (1 << length) + 1;varleft ="", right ="";for(vari = length - 1; i >= 0; i--) {// Mask to check if i't bit// is set or notvarmask = 1 << i;// If bit is set append '5' else append '4'varbit = mask & rank;if(bit) {left += y;right += y;}else{left += x;right += x;}}right = right.split('').reverse().join('');returnleft + right;}// Driver Codevarn = 23;varx ='4', y ='5';varans = solve(n, x, y);document.write( ans +"<br>");</script>Output54444445
Time Complexity:
where n is the length of string
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek! - The length (L) of the required Nth term:



