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.
- Numbers less than N which are product of exactly two distinct prime numbers
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N with digit sum greater than the digit sum of N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Find all factorial numbers less than or equal to n
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Print all numbers less than N with at-most 2 unique digits
- Print all Semi-Prime Numbers less than or equal to N
- Print all Strong numbers less than or equal to 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
- Minimum number of sets with numbers less than Y
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Find maximum product of digits among numbers less than or equal to N
- Sum of Semi-Prime Numbers less than or equal to N
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M