Smallest and Largest N-digit number starting and ending with N
Last Updated :
10 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
string findNumberL( int n)
{
if (n == 1)
return "1" ;
string result = "" ;
int length = ( int ) floor ( log10 (n) + 1);
for ( int i = 1; i <= n - (2 * length); i++)
{
result += '9' ;
}
result = to_string(n) + result + to_string(n);
return result;
}
string findNumberS( int n)
{
if (n == 1)
return "1" ;
string result = "" ;
int length = ( int ) floor ( log10 (n) + 1);
for ( int i = 1; i <= n - (2 * length); i++)
{
result += '0' ;
}
result = to_string(n) + result + to_string(n);
return result;
}
int main()
{
int N = 3;
cout << "Smallest Number = " << findNumberS(N) << endl;
cout << "Largest Number = " << findNumberL(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static String findNumberL( int n)
{
if (n == 1 )
return "1" ;
String result = "" ;
int length
= ( int )Math.floor(
Math.log10(n) + 1 );
for ( int i = 1 ;
i <= n - ( 2 * length); i++) {
result += '9' ;
}
result = Integer.toString(n)
+ result
+ Integer.toString(n);
return result;
}
static String findNumberS( int n)
{
if (n == 1 )
return "1" ;
String result = "" ;
int length
= ( int )Math.floor(
Math.log10(n) + 1 );
for ( int i = 1 ; i <= n - ( 2 * length); i++) {
result += '0' ;
}
result = Integer.toString(n)
+ result
+ Integer.toString(n);
return result;
}
public static void main(String[] args)
{
int N = 3 ;
System.out.println(
"Smallest Number = "
+ findNumberS(N));
System.out.print(
"Largest Number = "
+ findNumberL(N));
}
}
|
Python3
import math
def findNumberL(n):
if (n = = 1 ):
return "1"
result = ""
length = math.floor(math.log10(n) + 1 )
for i in range ( 1 , n - ( 2 *
length) + 1 ):
result + = '9'
result = ( str (n) + result +
str (n))
return result
def findNumberS(n):
if (n = = 1 ):
return "1"
result = ""
length = math.floor(math.log10(n) + 1 )
for i in range ( 1 , n -
( 2 * length) + 1 ):
result + = '0'
result = ( str (n) + result +
str (n))
return result
if __name__ = = "__main__" :
N = 3
print ( "Smallest Number = " + findNumberS(N))
print ( "Largest Number = " + findNumberL(N))
|
C#
using System;
class GFG{
static String findNumberL( int n)
{
if (n == 1)
return "1" ;
String result = "" ;
int length = ( int )Math.Floor(
Math.Log10(n) + 1);
for ( int i = 1;
i <= n - (2 * length); i++)
{
result += '9' ;
}
result = n.ToString() + result +
n.ToString();
return result;
}
static String findNumberS( int n)
{
if (n == 1)
return "1" ;
String result = "" ;
int length = ( int )Math.Floor(
Math.Log10(n) + 1);
for ( int i = 1;
i <= n - (2 * length); i++)
{
result += '0' ;
}
result = n.ToString() + result +
n.ToString();
return result;
}
public static void Main(String[] args)
{
int N = 3;
Console.WriteLine( "Smallest Number = " +
findNumberS(N));
Console.Write( "Largest Number = " +
findNumberL(N));
}
}
|
Javascript
<script>
function findNumberL(n)
{
if (n == 1)
return "1" ;
let result = "" ;
let length
= Math.floor(
Math.log10(n) + 1);
for (let i = 1;
i <= n - (2 * length); i++) {
result += '9' ;
}
result = n.toString()
+ result
+ n.toString();
return result;
}
function findNumberS(n)
{
if (n == 1)
return "1" ;
let result = "" ;
let length
= Math.floor(
Math.log10(n) + 1);
for (let i = 1; i <= n - (2 * length); i++) {
result += '0' ;
}
result = n.toString()
+ result
+ n.toString();
return result;
}
let N = 3;
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)
Share your thoughts in the comments
Please Login to comment...