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.
- Count of numbers in Array ending with digits of number N
- Represent a number as the sum of positive numbers ending with 9
- Find start and ending index of an element in an unsorted array
- Largest and smallest digit of a number
- Largest and Smallest N-digit Hexadecimal Numbers
- Largest and Smallest N-digit Octal Numbers
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Find the largest and second largest value in a Linked List
- Check if two people starting from different points ever meet
- Program to accept String starting with Capital letter
- Program to accept Strings starting with a Vowel
- Find smallest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Find the Largest number with given number of digits and sum of digits
- Find the smallest number whose digits multiply to a given number n
- Smallest number by rearranging digits of a given number
- Get the kth smallest number using the digits of the given number
- Immediate smallest number after re-arranging the digits of a given number
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest number formed by inverting digits of given number N
Improved By : amit143katiyar