Given an integer N, the task is to find the smallest and the largest N-digit numbers which start and ends with digit N.
Input: N = 3
Smallest Number = 303
Largest Number = 393
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
Smallest Number = 1
Largest Number = 1
1 is both the smallest and the largest 1 digit number which starts and ends with 1.
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:
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.
- Largest and smallest digit of a number
- Queries for the smallest and the largest prime number of given digit
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Minimum count of numbers required ending with 7 to sum as a given number
- Count of numbers in Array ending with digits of number N
- Smallest and Largest sum of two n-digit numbers
- Smallest and Largest Palindrome with N Digits
- Largest and Smallest N-digit Octal Numbers
- Smallest and Largest N-digit perfect cubes
- Largest and Smallest N-digit Hexadecimal Numbers
- Smallest and Largest N-digit perfect squares
- Sum and product of k smallest and k largest composite numbers in the array
- Sum and product of k smallest and k largest prime numbers in the array
- Minimum number of moves to reach N starting from (1, 1)
- Number of ways to reach (X, Y) in a matrix starting from the origin
- Number of ways to reach (M, N) in a matrix starting from the origin without visiting (X, Y)
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Smallest number dividing minimum number of elements in the array | Set 2
- Find smallest possible Number from a given large Number with same count of digits
- Smallest number dividing minimum number of elements in the Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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