Friday, September 27, 2024
Google search engine
HomeData Modelling & AISmallest and Largest N-digit number starting and ending with N

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 the 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:
 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find n digit
// largest number starting
// and ending with n
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)floor(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 = to_string(n) + result + to_string(n);
  
    // Return the largest number
    return result;
}
  
// Function to find n digit
// smallest number starting
// and ending with n
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)floor(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 = to_string(n) + result + to_string(n);
  
    // Return the smallest number
    return result;
}
 
// Driver code
int main()
{
   
    // Given number
    int N = 3;
  
    // Function call
    cout << "Smallest Number = " << findNumberS(N) << endl;
    cout << "Largest Number = " << findNumberL(N);
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java




// 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));
    }
}


Python3




# Python3 program for the
# above approach
import math
 
# Function to find n digit
# largest number starting
#and ending with n
def findNumberL(n):
   
    # Corner Case when n = 1
    if (n == 1):
        return "1"
 
    # Result will store the
    # n - 2*length(n) digit
    # largest number
    result = ""
 
    # Find the number of
    # digits in number n
    length  = math.floor(math.log10(n) + 1)
 
    # Append 9
    for i in range(1, n - (2 *
                   length) + 1):
        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 = (str(n) + result +
              str(n))
 
    # Return the largest number
    return result
 
# Function to find n digit
# smallest number starting
# and ending with n
def findNumberS(n):
 
    # Corner Case when n = 1
    if (n == 1):
            return "1"
 
    # Result will store the
    # n - 2*length(n) digit
    # smallest number
    result = ""
 
    # Find the number of
    # digits in number n
    length = math.floor(math.log10(n) + 1)
     
    for i in range(1, n -
                   (2 * length) + 1):
        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 = (str(n) + result +
              str(n))
 
    # Return the smallest number
    return result
 
# Driver Code
if __name__ == "__main__":
 
    # Given Number
    N = 3
 
    # Function Call
    print("Smallest Number = " + findNumberS(N))
    print("Largest Number = "+ findNumberL(N))
 
# This code is contributed by Chitranayal


C#




// 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


Javascript




<script>
 
// JavaScript program for the above approach
 
    // Function to find n digit
    // largest number starting
    // and ending with n
    function findNumberL(n)
    {
        // Corner Case when n = 1
        if (n == 1)
            return "1";
  
        // Result will store the
        // n - 2*length(n) digit
        // largest number
        let result = "";
  
        // Find the number of
        // digits in number n
        let length
            = Math.floor(
                Math.log10(n) + 1);
  
        // Append 9
        for (let 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
    function findNumberS(n)
    {
  
        // Corner Case when n = 1
        if (n == 1)
            return "1";
  
        // Result will store the
        // n - 2*length(n) digit
        // smallest number
        let result = "";
  
        // Find the number of
        // digits in number n
        let length
            = Math.floor(
                Math.log10(n) + 1);
        for (let 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
 
     // Given Number
        let N = 3;
  
        // Function Call
        document.write(
            "Smallest Number = "
            + findNumberS(N) + "<br/>");
        document.write(
            "Largest Number = "
            + findNumberL(N));
             
</script>


Output: 

Smallest Number = 303
Largest Number = 393

 

Time Complexity: O(N)

Space Complexity: O(N)

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!

RELATED ARTICLES

Most Popular

Recent Comments