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:



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find nth even
// palindromic number of only even
// length composing of 4's and 5's.
  
#include <bits/stdc++.h>
using namespace std;
  
// Utility function to compute
// n'th palindrome number
string solve(int n, char x, char y)
{
    // Calculate the length from above
    // formula as discussed above
    int length = ceil(log2(n + 2)) - 1;
  
    // Calculate rank for length L
    int rank = n - (1 << length) + 1;
  
    string left = "", right = "";
  
    for (int i = length - 1; i >= 0; i--) {
  
        // Mask to check if i't bit
        // is set or not
        int mask = 1 << i;
  
        // If bit is set append '5' else append '4'
        bool bit = mask & rank;
  
        if (bit) {
            left += y;
            right += y;
        }
        else {
            left += x;
            right += x;
        }
    }
  
    reverse(right.begin(), right.end());
  
    return left + right;
}
  
// Driver Code
int main()
{
    int n = 23;
    char x = '4', y = '5';
    string ans = solve(n, x, y);
    cout << ans << '\n';
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find nth even 
// palindromic number of only even 
// length composing of 4's and 5's. 
import java.util.*;
  
class GFG
{
      
    // Utility function to compute 
    // n'th palindrome number 
    static String solve(int n, char x, char y) 
    
        // Calculate the length from above 
        // formula as discussed above 
        int length = (int)Math.ceil(Math.log(n + 2) / 
                                    Math.log(2)) - 1
      
        // Calculate rank for length L 
        int rank = n - (1 << length) + 1
      
        String left = "", right = ""
      
        for (int i = length -1 ; i >= 0; i--)
        
      
            // Mask to check if i't bit 
            // is set or not 
            int mask = (1 << i); 
      
            // If bit is set append '5' else append '4' 
            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; 
    
      
    // Driver Code 
    public static void main (String[] args)
    
        int n = 23
        char x = '4', y = '5'
        String ans = solve(n, x, y); 
        System.out.println(ans); 
    
}
  
// This code is contributed by AnkitRai01
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find nth even 
# palindromic number of only even 
# length composing of 4's and 5's. 
from math import ceil, log2
  
# Utility function to compute 
# n'th palindrome number 
def solve(n, x, y) : 
  
    # Calculate the length from above 
    # formula as discussed above 
    length = ceil(log2(n + 2)) - 1
  
    # Calculate rank for length L 
    rank = n - (1 << length) + 1
  
    left = ""; right = ""; 
  
    for i in range(length - 1 , -1, -1):
  
        # Mask to check if i't bit 
        # is set or not 
        mask = (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;
    return res;
  
# Driver Code 
if __name__ == "__main__"
  
    n = 23
    x = '4';
    y = '5'
    ans = solve(n, x, y); 
    print(ans); 
      
# This code is contributed by kanugargng
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find nth even 
// palindromic number of only even 
// length composing of 4's and 5's. 
using System;
  
class GFG
{
      
    // Utility function to compute 
    // n'th palindrome number 
    static String solve(int n, char x, char y) 
    
        // Calculate the length from above 
        // formula as discussed above 
        int length = (int)Math.Ceiling(Math.Log(n + 2) / 
                                       Math.Log(2)) - 1; 
      
        // Calculate rank for length L 
        int rank = n - (1 << length) + 1; 
      
        String left = "", right = ""
      
        for (int i = length -1; i >= 0; i--)
        
      
            // Mask to check if i't bit 
            // is set or not 
            int mask = (1 << i); 
      
            // If bit is set append '5'
            // else append '4' 
            int bit = mask & rank; 
              
            if (bit > 0)
            
                left += y; 
                right += y; 
            
            else
            
                left += x; 
                right += x; 
            
        
          
        right = reverse(right);
        String res = left + right;
        return res; 
    
      
    static String reverse(String input) 
    {
        char[] a = input.ToCharArray();
        int l, r = 0;
        r = a.Length - 1;
  
        for (l = 0; l < r; l++, r--) 
        {
            // Swap values of l and r 
            char temp = a[l];
            a[l] = a[r];
            a[r] = temp;
        }
        return String.Join("", a);
    
      
    // Driver Code 
    public static void Main (String[] args)
    
        int n = 23; 
        char x = '4', y = '5'
        String ans = solve(n, x, y); 
        Console.WriteLine(ans); 
    
}
  
// This code is contributed by Rajput-Ji
chevron_right

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.




Recommended Posts:


Competitive Programmer, Full Stack Developer, Technical Content Writer, Machine Learner

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



Improved By : kanugargng, AnkitRai01, Rajput-Ji

Article Tags :
Practice Tags :