Find all numbers less than n, which are palindromic. Numbers can be printed in any order.
Input : n = 12 Output : 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 Input : n = 104 Output : 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101 [Note that below program prints these numbers in different order]
Brute Force: We check all the numbers from 1 to n whether its decimal representation is palindrome or not.
Efficient Approach: We start from 1 and create palindromes of odd digit and even digits up to n. For every number (starting from 1), we append its reverse at end if we need even length palindrome numbers. For odd length palindrome, we append reverse of all digits except last one.
11 22 33 44 55 66 77 88 99 1 2 3 4 5 6 7 8 9 101
Note that the above program doesn’t print output in sorted order. To print in sorted order, we can store palindromes in a vector and store it.
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Print all numbers less than N with at-most 2 unique digits
- Largest number less than N with digit sum greater than the digit sum of N
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Make palindromic string non-palindromic by rearranging its letters
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Generate a String of having N*N distinct non-palindromic Substrings
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Print a number strictly less than a given number such that all its digits are distinct.
- Sum of all odd length palindromic numbers within the range [L, R]
- Sum of all palindromic numbers lying in the range [L, R] for Q queries
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Highest power of 2 less than or equal to given number
- Check if frequency of each digit is less than the digit
- For each element in 1st array count elements less than or equal to it in 2nd array
- Largest number less than N whose each digit is prime number
- Smallest even digits number not less than N
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Smallest odd digits number not less than N