Smallest and Largest N-digit number starting and ending with N

Given an integer N, the task is to find the smallest and the largest N-digit numbers which start and ends with digit N.
Examples: 

Input: N = 3 
Output: 
Smallest Number = 303 
Largest Number = 393 
Explanation: 
303 is the smallest 3 digit number starting and ending with 3. 
393 is the largest 3 digit number starting and ending with 3.

Input: N = 1 
Output: 
Smallest Number = 1 
Largest Number = 1 
Explanation: 
1 is both the smallest and the largest 1 digit number which starts and ends with 1. 

Approach: 
We know that the largest and the smallest N-digit number is 9999…9, where 9 repeats N-times and 1000…. 0, where 0 repeats N-1 times respectively. 
Now to get the smallest and largest N-digit number starts and ends with N, we need to replace the first and the last digit of smallest and the largest N-digit number by N
We have to take care of corner case i.e., when N = 1, here both the largest and the smallest number will be 1

Below is the implementation of the above approach:



Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.io.*;
  
class GFG {
  
    // Function to find n digit
    // largest number starting
    // and ending with n
    static String findNumberL(int n)
    {
        // Corner Case when n = 1
        if (n == 1)
            return "1";
  
        // Result will store the
        // n - 2*length(n) digit
        // largest number
        String result = "";
  
        // Find the number of
        // digits in number n
        int length
            = (int)Math.floor(
                Math.log10(n) + 1);
  
        // Append 9
        for (int i = 1;
             i <= n - (2 * length); i++) {
            result += '9';
        }
  
        // To make it largest n digit
        // number starting and ending
        // with n, we just need to
        // append n at start and end
        result = Integer.toString(n)
                 + result
                 + Integer.toString(n);
  
        // Return the largest number
        return result;
    }
  
    // Function to find n digit
    // smallest number starting
    // and ending with n
    static String findNumberS(int n)
    {
  
        // Corner Case when n = 1
        if (n == 1)
            return "1";
  
        // Result will store the
        // n - 2*length(n) digit
        // smallest number
        String result = "";
  
        // Find the number of
        // digits in number n
        int length
            = (int)Math.floor(
                Math.log10(n) + 1);
        for (int i = 1; i <= n - (2 * length); i++) {
            result += '0';
        }
  
        // To make it smallest n digit
        // number starting and ending
        // with n, we just need to
        // append n at start and end
        result = Integer.toString(n)
                 + result
                 + Integer.toString(n);
  
        // Return the smallest number
        return result;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Given Number
        int N = 3;
  
        // Function Call
        System.out.println(
            "Smallest Number = "
            + findNumberS(N));
        System.out.print(
            "Largest Number = "
            + findNumberL(N));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
  
class GFG{
  
// Function to find n digit
// largest number starting
// and ending with n
static String findNumberL(int n)
{
      
    // Corner Case when n = 1
    if (n == 1)
        return "1";
  
    // Result will store the
    // n - 2*length(n) digit
    // largest number
    String result = "";
  
    // Find the number of
    // digits in number n
    int length = (int)Math.Floor(
                      Math.Log10(n) + 1);
  
    // Append 9
    for(int i = 1; 
            i <= n - (2 * length); i++)
    {
        result += '9';
    }
  
    // To make it largest n digit
    // number starting and ending
    // with n, we just need to
    // append n at start and end
    result = n.ToString() + result + 
             n.ToString();
  
    // Return the largest number
    return result;
}
  
// Function to find n digit
// smallest number starting
// and ending with n
static String findNumberS(int n)
{
  
    // Corner Case when n = 1
    if (n == 1)
        return "1";
  
    // Result will store the
    // n - 2*length(n) digit
    // smallest number
    String result = "";
  
    // Find the number of
    // digits in number n
    int length = (int)Math.Floor(
                      Math.Log10(n) + 1);
    for (int i = 1; 
             i <= n - (2 * length); i++)
    {
        result += '0';
    }
  
    // To make it smallest n digit
    // number starting and ending
    // with n, we just need to
    // append n at start and end
    result = n.ToString() + result + 
             n.ToString();
  
    // Return the smallest number
    return result;
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given number
    int N = 3;
  
    // Function call
    Console.WriteLine("Smallest Number = "
                       findNumberS(N));
    Console.Write("Largest Number = " +
                   findNumberL(N));
}
}
  
// This code is contributed by Amit Katiyar

chevron_right


Output: 

Smallest Number = 303
Largest Number = 393

Time Complexity: O(N)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : amit143katiyar