Nth Even length Palindrome

2.9

Given a number n as 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 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 implementation of this approach .

C/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 to test above function
int main()
{
	string n = "10";
	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 program
	public static void main (String[] args) 
	{
		String n = "10";
		System.out.println(evenlength(n));
	}
}

// Contributed by Pramod Kumar


Output:

 1001

Time Complexity : O(n)
This article is contributed by Surya Priy. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2.9 Average Difficulty : 2.9/5.0
Based on 13 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.