# Find the Kth smallest odd length palindrome number

• Difficulty Level : Easy
• Last Updated : 22 Jul, 2021

Given a positive integer K, the task is to find Kth smallest palindromic number odd length.

Examples:

Input: K = 5
Output: 5
Explanation:
The palindromic numbers of odd lengths is {1, 2, 3, 4, 5, 6, 7, …, }. The 5th smallest palindromic numbers is 5.

Input: K = 10
Output: 101

Approach: The given problem can be solved based on the following observations:

• The first Palindromic Numbers of length 1 are 1, 2, 3, 4, 5, 6, 7, 8, and 9.
• The first Palindromic Numbers of length 3 is 101, which is the 10th smallest odd length palindrome number. Similarly, 11th, 12th, 13th, …, 99th smallest palindromic numbers are 111, 121, 131 …, 999 respectively.
• Therefore, the Kth smallest odd length palindrome number can be formed by joining K and the reverse of K except the last digit.

From the above observations, the Kth smallest odd length palindromic number is given by appending the reverse of all the digits of K except the last one at the end of K.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the Kth smallest``// odd length palindrome``int` `oddLengthPalindrome(``int` `k)``{``    ` `    ``// Store the original number K``    ``int` `palin = k;` `    ``// Removing the last digit of K``    ``k = k / 10;` `    ``// Generate the palindrome by``    ``// appending the reverse of K``    ``// except last digit to itself``    ``while` `(k > 0)``    ``{``        ` `        ``// Find the remainder``        ``int` `rev = k % 10;` `        ``// Add the digit to palin``        ``palin = (palin * 10) + rev;` `        ``// Divide K by 10``        ``k = k / 10;``    ``}` `    ``// Return the resultant palindromic``    ``// number formed``    ``return` `palin;``}` `// Driver Code``int` `main()``{``    ``int` `k = 504;` `    ``cout << oddLengthPalindrome(k);``}` `// This code is contributed by rishavmahato348`

## Java

 `// Java program for the above approach``import` `java.util.*;``import` `java.lang.*;` `class` `GFG{``    ` `// Function to find the Kth smallest``// odd length palindrome``static` `int` `oddLengthPalindrome(``int` `k)``{``    ` `    ``// Store the original number K``    ``int` `palin = k;``    ` `    ``// Removing the last digit of K``    ``k = k / ``10``;``    ` `    ``// Generate the palindrome by``    ``// appending the reverse of K``    ``// except last digit to itself``    ``while` `(k > ``0``)``    ``{ ``        ` `        ``// Find the remainder``        ``int` `rev = k % ``10``;``        ` `        ``// Add the digit to palin``        ``palin = (palin * ``10``) + rev;``        ` `        ``// Divide K by 10``        ``k = k / ``10``;``    ``}``    ` `    ``// Return the resultant palindromic``    ``// number formed``    ``return` `palin;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `k = ``504``;``    ` `    ``System.out.println(oddLengthPalindrome(k));``}``}` `// This code is contributed by Sudhanshu Bhagat & Govind Choudhary`

## Python3

 `# Python3 program for the above approach` `# Function to find the Kth smallest``# odd length palindrome number``def` `oddLengthPalindrome(K):` `    ``# Store the original number K``    ``palin ``=` `K` `    ``# Removing the last digit of K``    ``K ``=` `K ``/``/` `10` `    ``# Generate the palindrome by``    ``# appending the reverse of K``    ``# except last digit to itself``    ``while` `(K > ``0``):``      ` `        ``# Find the remainder``        ``rev ``=` `K ``%` `10``        ` `        ``# Add the digit to palin``        ``palin ``=` `palin ``*` `10` `+` `rev``        ` `        ``# Divide K by 10``        ``K ``=` `K ``/``/` `10` `    ``# Return the resultant palindromic``    ``# number formed``    ``return` `palin` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:` `    ``K ``=` `504``    ``print``(oddLengthPalindrome(K))``    ` `    ` `#Contributed by Govind Choudhary & Pallav Pushparaj`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``    ` `// Function to find the Kth smallest``// palindrome of odd length``static` `int` `oddLengthPalindrome(``int` `k)``{``    ` `    ``// Store the original number K``    ``int` `palin = k;``    ` `    ``// Removing the last digit of K``    ``k = k / 10;``    ` `    ``// Generate the palindrome by``    ``// appending the reverse of K``    ``// except last digit to itself``    ``while` `(k > 0)``    ``{``        ` `        ``// Find the remainder``        ``int` `rev = k % 10;``        ` `        ``// Add the digit to palin``        ``palin = (palin * 10) + rev;``        ` `        ``// Divide K by 10``        ``k = k / 10;``    ``}``    ` `    ``// Return the resultant palindromic``    ``// number formed``    ``return` `palin;``}` `// Driver Code``static` `void` `Main(``string``[] args)``{``    ``int` `k = 504;` `    ``Console.WriteLine(oddLengthPalindrome(k));``}``}` `// This code is contributed by Sudhanshu Bhagat & Govind Choudhary`

## Javascript

 ``

Output:

`50405`

Time Complexity: O(log10K)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up