Nth Even length Palindrome

Given a number n as a string, find the nth even-length positive palindrome number .

Examples: 

Input : n = "1"
Output : 11
1st even-length palindrome is 11 .

Input : n = "10"
Output : 1001
The first 10 even-length palindrome numbers are 11, 22, 
33, 44, 55, 66, 77, 88, 99 and 1001.

As, it is a even-length palindrome so its first half should be equal to reverse of second half and length will be 2, 4, 6, 8 …. To evaluate nth palindrome let’s just see 1st 10 even-length palindrome numbers 11, 22, 33, 44, 55, 66, 77, 88, 99 and 1001 . Here, nth palindrome is nn’ where n’ is reverse of n . Thus we just have to write n and n’ in a consecutive manner where n’ is reverse of n .

Below is the implementation of this approach.

C++




// C++ program to find n=th even length string.
#include <bits/stdc++.h>
using namespace std;
 
// Function to find nth even length Palindrome
string evenlength(string n)
{
    // string r to store resultant
    // palindrome. Initialize same as s
    string res = n;
 
    // In this loop string r stores
    // reverse of string s after the
    // string s in consecutive manner .
    for (int j = n.length() - 1; j >= 0; --j)
        res += n[j];
 
    return res;
}
 
// Driver code
int main()
{
    string n = "10";
   
    // Function call
    cout << evenlength(n);
    return 0;
}


Java




// Java program to find nth even length Palindrome
import java.io.*;
 
class GFG
{
    // Function to find nth even length Palindrome
    static String evenlength(String n)
    {
        // string r to store resultant
        // palindrome. Initialize same as s
        String res = n;
 
        // In this loop string r stores
        // reverse of string s after the
        // string s in consecutive manner
        for (int j = n.length() - 1; j >= 0; --j)
            res += n.charAt(j);
 
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String n = "10";
       
        // Function call
        System.out.println(evenlength(n));
    }
}
 
// Contributed by Pramod Kumar


Python3




# Python3 program to find n=th even
# length string.
import math as mt
 
# Function to find nth even length
# Palindrome
 
 
def evenlength(n):
 
    # string r to store resultant
    # palindrome. Initialize same as s
    res = n
 
    # In this loop string r stores
    # reverse of string s after the
    # string s in consecutive manner .
    for j in range(len(n) - 1, -1, -1):
        res += n[j]
 
    return res
 
 
# Driver code
n = "10"
 
# Function call
print(evenlength(n))
 
# This code is contributed by
# Mohit kumar 29


C#




// C# program to find nth even
// length Palindrome
using System;
 
class GFG {
 
    // Function to find nth even
    // length Palindrome
    static string evenlength(string n)
    {
 
        // string r to store resultant
        // palindrome. Initialize same
        // as s
        string res = n;
 
        // In this loop string r stores
        // reverse of string s after
        // the string s in consecutive
        // manner
        for (int j = n.Length - 1; j >= 0; --j)
            res += n[j];
 
        return res;
    }
 
    // Driver code
    public static void Main()
    {
        string n = "10";
 
        // Function call
        Console.WriteLine(evenlength(n));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program to find n=th even length string.
 
// Function to find nth even length Palindrome
function evenlength($n)
{
    // string r to store resultant
    // palindrome. Initialize same as s
    $res = $n;
 
    // In this loop string r stores
    // reverse of string s after the
    // string s in consecutive manner .
    for ($j = strlen($n) - 1; $j >= 0; --$j)
        $res = $res . $n[$j];
 
    return $res;
}
 
// Driver code
$n = "10";
 
// Function call
echo evenlength($n);
 
// This code is contributed by ita_c
?>


Javascript




<script>
// Javascript program to find nth even length Palindrome
     
    // Function to find nth even length Palindrome
    function evenlength(n)
    {
        // string r to store resultant
        // palindrome. Initialize same as s
        let res = n;
  
        // In this loop string r stores
        // reverse of string s after the
        // string s in consecutive manner
        for (let j = n.length - 1; j >= 0; --j)
            res += n[j];
  
        return res;
    }
     
    // Driver code
    let n = "10";
    // Function call
    document.write(evenlength(n));
     
    //This code is contributed by avanitrachhadiya2155
     
</script>


Output

1001

Time Complexity: O(n) 
Auxiliary Space: O(n)

This article is contributed by Aarti_Rathi and Surya Priy. If you like zambiatek and would like to contribute, you can also write an article using write.zambiatek.com or mail your article to review-team@zambiatek.com. See your article appearing on the zambiatek main page and help other Geeks.

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!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button