Given a number N. The task is to find the smallest and largest palindromic number possible with N digits.
Input: N = 4 Output: Smallest Palindrome = 1001 Largest Palindrome = 9999 Input: N = 5 Output: Smallest Palindrome = 10001 Largest Palindrome = 99999
Smallest N-digit Palindromic Number: On observing carefully, you will observe that for N = 1, the smallest palindromic number will be 0. And for any other value of N, the smallest palindrome will have the first and last digits as 1 and all of the digits in between as 0.
- Case 1 : If N = 1 then answer will be 0.
- Case 2 : If N != 1 then answer will be (10(N-1)) + 1.
Largest N-digit Palindromic Number: Similar to the above approach, you can see that the largest possible palindrome number with N-digits can be obtained by appending 9 for N times. Therefore, largest N digits palindrome number will be 10N – 1.
Below is the implementation of the above approach:
Smallest Palindrome: 1001 Largest Palindrome: 9999
Time Complexity: O(1)
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.
- Make largest palindrome by changing at most K-digits
- Next smallest prime palindrome
- N'th palindrome of K digits
- Construct lexicographically smallest palindrome
- Given a number, find the next smallest palindrome
- Check if the sum of digits of N is palindrome
- Make a lexicographically smallest palindrome with minimal changes
- Find all palindrome numbers of given digits
- Largest palindrome which is product of two n-digit numbers
- Largest palindrome which is product of two N-digit numbers : Set 2
- Make lexicographically smallest palindrome by substituting missing characters
- Count the number of digits of palindrome numbers in an array
- Find smallest number with given number of digits and sum of digits under given constraints
- Smallest x such that 1*n, 2*n, ... x*n have all digits from 1 to 9
- Smallest odd number with N digits
- Smallest odd digits number not less than N
- Smallest even digits number not less than N
- Smallest Even number with N digits
- Largest and smallest digit of a number
- Smallest and Largest sum of two n-digit numbers
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.