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

• Difficulty Level : Easy
• Last Updated : 07 May, 2021

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

 ``

Output:

```Smallest Number = 303
Largest Number = 393```

Time Complexity: O(N)

My Personal Notes arrow_drop_up