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.
Input: N = 9, X = 4, Y = 5
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
Even length palindromic numbers using 1 & 2 are
11, 22, 1111, 1221, 2112, 2222, 111111, 112211, 121121, …
6th term of the above series = 2222
- 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
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
If N = 9, -> L = 2(ceil(log2(N + 2)) - 1) = 6 -> R = N - 2L/2 + 1 = 2 -> First Half = Binary (2) = 010 = XYX -> Second Half = Reverse (First Half) = XYX -> Complete Nth term = First Half + Second Half = XYXXYX which is the required Nth term.
- The length (L) of the required Nth term:
Below is the implementation of the above approach:
Time Complexity: where n is the length of string
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count numbers from given range having odd digits at odd places and even digits at even places
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Number of digits in the nth number made of given four digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Find maximum number that can be formed using digits of a given number
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Check if a string contains a palindromic sub-string of even length
- Check if a string can be split into even length palindromic substrings
- Longest palindromic String formed using concatenation of given strings in any order
- Print all possible palindromic string formed using any pair of given strings
- Find smallest number formed by inverting digits of given number N
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Nth Even length Palindrome
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.