Generate all palindromic numbers less than n
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 sort it, and don’t forget to use the required header file.
- First, we define a function isPalindrome() which takes an integer n as input and returns a boolean value indicating whether it is palindrome or not.
- To check if the number is palindrome, we convert it to a string using to_string() function, then we iterate over its first half and compare it with the reversed second half.
- If the first half is not equal to the reversed second half, the function returns false. Otherwise, it returns true.
- Next, we define the function generatePalindromes() which takes an integer n as input and generates and prints all palindromic numbers less than n.
- We iterate over all numbers from 1 to n-1 using a for loop and check if each number is palindrome or not using the isPalindrome() function.
- If a number is palindrome, we print it using the cout statement.
- Finally, we call the generatePalindromes() function with the given value of n to print all palindromic numbers less than n.
Below is the implementation of the above approach:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101
Time Complexity: O(n * log10(n)) because we iterate over all numbers from 1 to n-1, which takes O(n) time, and for each number, we need to convert it into a string using to_string() function, which takes O(log10(n)) time, as the number of digits in a number is given by log10(n). Therefore, the overall time complexity is O(n * log10(n)).
Space Complexity: O(log10(n)) because we need to store the string representation of each number, which has a maximum length of log10(n). Therefore, the overall space complexity is O(log10(n)).
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 write.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.
Please Login to comment...