Given two integers n and k, Find the lexicographical nth palindrome of k digits.
Examples:
Input : n = 5, k = 4
Output : 1441
Explanation:
4 digit lexicographical palindromes are:
1001, 1111, 1221, 1331, 1441
5th palindrome = 1441
Input : n = 4, k = 6
Output : 103301
Naive Approach
A brute force is to run a loop from the smallest kth digit number and check for every number whether it is palindrome or not. If it is a palindrome number then decrements the value of k. Therefore the loop runs until k becomes exhausted.
C++
#include<bits/stdc++.h>
using namespace std;
int reverseNum( int n)
{
int rem, rev=0;
while (n)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
return rev;
}
bool isPalindrom( int num)
{
return num == reverseNum(num);
}
int nthPalindrome( int n, int k)
{
int num = ( int ) pow (10, k-1);
while ( true )
{
if (isPalindrom(num))
--n;
if (!n)
break ;
++num;
}
return num;
}
int main()
{
int n = 6, k = 5;
printf ( "%dth palindrome of %d digit = %d\n" ,
n, k, nthPalindrome(n, k));
n = 10, k = 6;
printf ( "%dth palindrome of %d digit = %d" ,
n, k, nthPalindrome(n, k));
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int reverseNum( int n)
{
int rem, rev = 0 ;
while (n > 0 )
{
rem = n % 10 ;
rev = rev * 10 + rem;
n /= 10 ;
}
return rev;
}
static boolean isPalindrom( int num)
{
return num == reverseNum(num);
}
static int nthPalindrome( int n, int k)
{
int num = ( int )Math.pow( 10 , k- 1 );
while ( true )
{
if (isPalindrom(num))
--n;
if (n == 0 )
break ;
++num;
}
return num;
}
public static void main(String[] args)
{
int n = 6 , k = 5 ;
System.out.println(n + "th palindrome of " + k + " digit = " + nthPalindrome(n, k));
n = 10 ; k = 6 ;
System.out.println(n + "th palindrome of " + k + " digit = " + nthPalindrome(n, k));
}
}
|
Python3
import math;
def reverseNum(n):
rev = 0 ;
while (n):
rem = n % 10 ;
rev = (rev * 10 ) + rem;
n = int (n / 10 );
return rev;
def isPalindrom(num):
return num = = reverseNum(num);
def nthPalindrome(n, k):
num = math. pow ( 10 , k - 1 );
while ( True ):
if (isPalindrom(num)):
n - = 1 ;
if ( not n):
break ;
num + = 1 ;
return int (num);
n = 6 ;
k = 5 ;
print (n, "th palindrome of" ,k, "digit =" ,nthPalindrome(n, k));
n = 10 ;
k = 6 ;
print (n, "th palindrome of" ,k, "digit =" ,nthPalindrome(n, k));
|
C#
using System;
class GFG
{
static int reverseNum( int n)
{
int rem, rev = 0;
while (n > 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
return rev;
}
static bool isPalindrom( int num)
{
return num == reverseNum(num);
}
static int nthPalindrome( int n, int k)
{
int num = ( int )Math.Pow(10, k-1);
while ( true )
{
if (isPalindrom(num))
--n;
if (n == 0)
break ;
++num;
}
return num;
}
public static void Main()
{
int n = 6, k = 5;
Console.WriteLine(n + "th palindrome of " + k + " digit = " + nthPalindrome(n, k));
n = 10; k = 6;
Console.WriteLine(n + "th palindrome of " + k + " digit = " + nthPalindrome(n, k));
}
}
|
PHP
<?php
function reverseNum( $n )
{
$rem ;
$rev = 0;
while ( $n )
{
$rem = $n % 10;
$rev = ( $rev * 10) + $rem ;
$n = (int)( $n / 10);
}
return $rev ;
}
function isPalindrom( $num )
{
return $num == reverseNum( $num );
}
function nthPalindrome( $n , $k )
{
$num = pow(10, $k - 1);
while (true)
{
if (isPalindrom( $num ))
-- $n ;
if (! $n )
break ;
++ $num ;
}
return $num ;
}
$n = 6;
$k = 5;
echo $n , "th palindrome of " , $k , " digit = " ,
nthPalindrome( $n , $k ), "\n" ;
$n = 10;
$k = 6;
echo $n , "th palindrome of " , $k , " digit = " ,
nthPalindrome( $n , $k ), "\n" ;
?>
|
Javascript
<script>
function reverseNum(n)
{
let rem, rev = 0;
while (n > 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n = parseInt(n / 10);
}
return rev;
}
function isPalindrom(num)
{
return num == reverseNum(num);
}
function nthPalindrome(n, k)
{
let num = Math.pow(10, k-1);
while ( true )
{
if (isPalindrom(num))
--n;
if (n == 0)
break ;
++num;
}
return num;
}
let n = 6, k = 5;
document.write(n + "th palindrome of " + k +
" digit = " + nthPalindrome(n, k) + "</br>" );
n = 10; k = 6;
document.write(n + "th palindrome of " + k +
" digit = " + nthPalindrome(n, k));
</script>
|
Output:
6th palindrome of 5 digit = 10501
10th palindrome of 6 digit = 109901
Time complexity: O(10k)
Auxiliary space: O(1), since no extra space has been taken.
Efficient approach
An efficient method is to look for a pattern. According to the property of palindrome first, half digits are the same as the rest half digits in reverse order. Therefore, we only need to look for the first half digits as the rest of them can easily be generated. Let’s take k = 8, the smallest palindrome always starts from 1 as the leading digit and goes like that for the first 4 digits of the number.
First half values for k = 8
1st: 1000
2nd: 1001
3rd: 1002
...
...
100th: 1099
So we can easily write the above sequence for nth
palindrome as: (n-1) + 1000
For k digit number, we can generalize above formula as:
If k is odd
=> num = (n-1) + 10k/2
else
=> num = (n-1) + 10k/2 - 1
Now rest half digits can be expanded by just
printing the value of num in reverse order.
But before this if k is odd then we have to truncate
the last digit of a value num
Illustration:
n = 6 k = 5
- Determine the number of first half digits = floor(5/2) = 2
- Use formula: num = (6-1) + 102 = 105
- Expand the rest half digits by reversing the value of num.
Final answer will be 10501
Below is the implementation of the above steps
C++
#include<bits/stdc++.h>
using namespace std;
void nthPalindrome( int n, int k)
{
int temp = (k & 1) ? (k / 2) : (k/2 - 1);
int palindrome = ( int ) pow (10, temp);
palindrome += n - 1;
printf ( "%d" , palindrome);
if (k & 1)
palindrome /= 10;
while (palindrome)
{
printf ( "%d" , palindrome % 10);
palindrome /= 10;
}
printf ( "\n" );
}
int main()
{
int n = 6, k = 5;
printf ( "%dth palindrome of %d digit = " ,n ,k);
nthPalindrome(n ,k);
n = 10, k = 6;
printf ( "%dth palindrome of %d digit = " ,n ,k);
nthPalindrome(n, k);
return 0;
}
|
Java
class GFG{
static void nthPalindrome( int n, int k)
{
int temp = (k & 1 )!= 0 ? (k / 2 ) : (k/ 2 - 1 );
int palindrome = ( int )Math.pow( 10 , temp);
palindrome += n - 1 ;
System.out.print(palindrome);
if ((k & 1 )> 0 )
palindrome /= 10 ;
while (palindrome> 0 )
{
System.out.print(palindrome % 10 );
palindrome /= 10 ;
}
System.out.println( "" );
}
public static void main(String[] args)
{
int n = 6 , k = 5 ;
System.out.print(n+ "th palindrome of " +k+ " digit = " );
nthPalindrome(n ,k);
n = 10 ;
k = 6 ;
System.out.print(n+ "th palindrome of " +k+ " digit = " );
nthPalindrome(n, k);
}
}
|
Python3
def nthPalindrome(n, k):
if (k & 1 ):
temp = k / / 2
else :
temp = k / / 2 - 1
palindrome = 10 * * temp
palindrome = palindrome + n - 1
print (palindrome, end = "")
if (k & 1 ):
palindrome = palindrome / / 10
while (palindrome):
print (palindrome % 10 , end = "")
palindrome = palindrome / / 10
if __name__ = = '__main__' :
n = 6
k = 5
print (n, "th palindrome of" , k, " digit = " , end = " " )
nthPalindrome(n, k)
print ()
n = 10
k = 6
print (n, "th palindrome of" , k, "digit = " ,end = " " )
nthPalindrome(n, k)
|
C#
using System;
class GFG
{
static void nthPalindrome( int n, int k)
{
int temp = (k & 1) != 0 ? (k / 2) : (k / 2 - 1);
int palindrome = ( int )Math.Pow(10, temp);
palindrome += n - 1;
Console.Write(palindrome);
if ((k & 1) > 0)
palindrome /= 10;
while (palindrome>0)
{
Console.Write(palindrome % 10);
palindrome /= 10;
}
Console.WriteLine( "" );
}
static public void Main ()
{
int n = 6, k = 5;
Console.Write(n+ "th palindrome of " + k +
" digit = " );
nthPalindrome(n, k);
n = 10;
k = 6;
Console.Write(n+ "th palindrome of " + k +
" digit = " );
nthPalindrome(n, k);
}
}
|
PHP
<?php
function nthPalindrome( $n , $k )
{
$temp = ( $k & 1) ?
(int)( $k / 2) : (int)( $k / 2 - 1);
$palindrome = (int)pow(10, $temp );
$palindrome += $n - 1;
print ( $palindrome );
if ( $k & 1)
$palindrome = (int)( $palindrome / 10);
while ( $palindrome > 0)
{
print ( $palindrome % 10);
$palindrome = (int)( $palindrome / 10);
}
print ( "\n" );
}
$n = 6;
$k = 5;
print ( $n . "th palindrome of $k digit = " );
nthPalindrome( $n , $k );
$n = 10;
$k = 6;
print ( $n . "th palindrome of $k digit = " );
nthPalindrome( $n , $k );
?>
|
Javascript
<script>
function nthPalindrome(n, k)
{
let temp = (k & 1) != 0 ? parseInt(k / 2, 10) : (parseInt(k / 2, 10) - 1);
let palindrome = parseInt(Math.pow(10, temp), 10);
palindrome += n - 1;
document.write(palindrome);
if ((k & 1) > 0)
palindrome = parseInt(palindrome / 10, 10);
while (palindrome>0)
{
document.write(palindrome % 10);
palindrome = parseInt(palindrome / 10, 10);
}
document.write( "" + "</br>" );
}
let n = 6, k = 5;
document.write(n+ "th palindrome of " + k + " digit = " );
nthPalindrome(n, k);
n = 10;
k = 6;
document.write(n+ "th palindrome of " + k + " digit = " );
nthPalindrome(n, k);
</script>
|
Output:
6th palindrome of 5 digit = 10501
10th palindrome of 6 digit = 109901
Time complexity: O(k)
Auxiliary space: O(1), since no extra space has been taken.
Reference:
http://stackoverflow.com/questions/11925840/how-to-calculate-nth-n-digit-palindrome-efficiently
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
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 geeks!
Last Updated :
13 Sep, 2023
Like Article
Save Article