Número de N dígitos más pequeño y más grande que comienza y termina con N

Dado un número entero N , la tarea es encontrar los números de N dígitos más pequeños y más grandes que comienzan y terminan con el dígito N.
Ejemplos: 

Entrada: N = 3 
Salida: 
Número más pequeño = 303 
Número más grande = 393 
Explicación: 
303 es el número de 3 dígitos más pequeño que comienza y termina en 3. 
393 es el número de 3 dígitos más grande que comienza y termina en 3.
Entrada: N = 1 
Salida: 
Número más pequeño = 1 
Número más grande = 1 
Explicación: 
1 es tanto el número de 1 dígito más pequeño como el más grande que comienza y termina en 1. 
 

Enfoque: 
sabemos que el número de N dígitos más grande y más pequeño es 9999…9 , donde 9 se repite N veces y 1000…. 0 , donde 0 se repite N-1 veces respectivamente. 
Ahora, para obtener el número de N dígitos más pequeño y más grande que comienza y termina con N , necesitamos reemplazar el primer y el último dígito del número de N dígitos más pequeño y más grande por N
Tenemos que cuidar el caso de la esquina, es decir, cuando N = 1 , aquí tanto el número más grande como el más pequeño serán 1
A continuación se muestra la implementación del enfoque anterior:
 

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>
Producción: 

Smallest Number = 303
Largest Number = 393

 

Complejidad de tiempo: O(N)

Publicación traducida automáticamente

Artículo escrito por shubham prakash 1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *