Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmSmallest and Largest Palindrome with N Digits

Smallest and Largest Palindrome with N Digits

Given a number N. The task is to find the smallest and largest palindromic number possible with N digits.
Examples: 
 

Input: N = 4 
Output: 
Smallest Palindrome = 1001
Largest Palindrome = 9999

Input: N = 5
Output: 
Smallest Palindrome = 10001
Largest Palindrome = 99999

 

Smallest N-digit Palindromic Number: On observing carefully, you will observe that for N = 1, the smallest palindromic number will be 0. And for any other value of N, the smallest palindrome will have the first and last digits as 1 and all of the digits in between as 0. 
 

  • Case 1 : If N = 1 then answer will be 0.
  • Case 2 : If N != 1 then answer will be (10(N-1)) + 1.

Largest N-digit Palindromic Number: Similar to the above approach, you can see that the largest possible palindrome number with N-digits can be obtained by appending 9 for N times. Therefore, largest N digits palindrome number will be 10N – 1.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the smallest and largest
// palindrome with N digits
void printPalindrome(int n)
{
    if (n == 1)
    {
        cout<<"Smallest Palindrome: 0"<<endl;
        cout<<"Largest Palindrome: 9";
    }
    else
    {
        cout<<"Smallest Palindrome: "<<pow(10, n - 1) + 1;
        cout<<"\nLargest Palindrome: "<<pow(10,n) - 1;
    }
}
 
// Driver Code
int main()
{
    int n = 4;
    printPalindrome(n);
 
    return 0;
}


Java




// Java implementation of the above approach
class GfG {
 
    // Function to print the smallest and largest
    // palindrome with N digits
    static void printPalindrome(int n)
    {
        if (n == 1)
        {
            System.out.println("Smallest Palindrome: 0");
            System.out.println("Largest Palindrome: 9");
        }
        else
        {
            System.out.println("Smallest Palindrome: "
                    + (int)(Math.pow(10, n - 1)) + 1);
                     
            System.out.println("Largest Palindrome: "
                    + ((int)(Math.pow(10,n)) - 1));
        }
    }
     
    // Driver Code
    public static void main(String[] args) {
        int n = 4;
        printPalindrome(n);
    }
}


Python3




# Python 3 implementation of the above approach
 
from math import pow
 
# Function to print the smallest and largest
# palindrome with N digits
def printPalindrome(n):
    if (n == 1):
        print("Smallest Palindrome: 0")
        print("Largest Palindrome: 9")
    else:
        print("Smallest Palindrome:", int(pow(10, n - 1))+1)
        print("Largest Palindrome:", int(pow(10,n))-1)
     
 
# Driver Code
if __name__ == '__main__':
    n = 4
    printPalindrome(n)
 
# This code is contributed by
# Surendra_Gangwar


C#




// C# implementation of the approach
using System;
 
class GfG
{
 
    // Function to print the smallest and largest
    // palindrome with N digits
    static void printPalindrome(int n)
    {
        if (n == 1)
        {
            Console.WriteLine("Smallest Palindrome: 0");
            Console.WriteLine("Largest Palindrome: 9");
        }
        else
        {
            Console.WriteLine("Smallest Palindrome: "
                    + (int)(Math.Pow(10, n - 1)) + 1);
                     
            Console.WriteLine("Largest Palindrome: "
                    + ((int)(Math.Pow(10,n)) - 1));
        }
    }
     
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 4;
        printPalindrome(n);
    }
}
 
/* This code contributed by PrinciRaj1992 */


PHP




<?php
// PHP implementation of the above approach
 
// Function to print the smallest and largest
// palindrome with N digits
function printPalindrome($n)
{
    if ($n == 1)
    {
        echo "Smallest Palindrome: 0\n";
        echo "Largest Palindrome: 9";
    }
    else
    {
        echo "Smallest Palindrome: ",
                 pow(10, $n - 1) + 1;
        echo "\nLargest Palindrome: ",
                      pow(10, $n) - 1;
    }
}
 
// Driver Code
$n = 4;
printPalindrome($n);
 
// This code is contributed by ihritik
?>


Javascript




  <script>
    // Javascript implementation of the above approach
 
    // Function to print the smallest and largest
    // palindrome with N digits
    function printPalindrome(n)
    {
      if (n == 1)
      {
        document.write("Smallest Palindrome: 0<br>");
        document.write("Largest Palindrome: 9");
      }
      else
      {
        document.write("Smallest Palindrome: " + (parseInt(Math.pow(10, n - 1)) + 1));
        document.write("<br>Largest Palindrome: " + parseInt(Math.pow(10, n) - 1));
      }
    }
 
    // Driver Code
    var n = 4;
    printPalindrome(n);
 
// This code is contributed by rrrtnx.
  </script>


Output: 

Smallest Palindrome: 1001
Largest Palindrome: 9999

 

Time Complexity: O(logn)

Auxiliary Space: O(1)
 

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 neveropen!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments