Given a number N, we have to find the minimum number of palindromes required to express N as a sum of them.
Input : N = 11
Output : 1
Explanation: 11 is itself a palindrome.
Input : N = 65
Output : 3
Explanation: 65 can be expressed as a sum of three palindromes (55, 9, 1).
In the previous post, we discussed a dynamic programming approach to this problem which had a time and space complexity of O(N3/2).
Cilleruelo, Luca, and Baxter proved in a 2016 research paper that every number can be expressed as the sum of a maximum of three palindromes in any base b >= 5 (this lower bound was later improved to 3). For the proof of this theorem, please refer to the original paper. We can make the use of this theorem by safely assuming the answer to be three if the number N is not itself a palindrome and cannot be expressed as the sum of two palindromes.
Below is the implementation of the above approach:
Time Complexity: O(√(N)log 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.
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of distinct powers of 2 required to express a given binary number
- Program to print all palindromes in a given range
- Count maximum-length palindromes in a String
- Count special palindromes in a String
- Check if suffix and prefix of a string are palindromes
- Count of ways to split given string into two non-empty palindromes
- Count of minimum reductions required to get the required sum K
- Minimum numbers needed to express every integer below N as a sum
- Express a number as sum of consecutive numbers
- Count ways to express a number as sum of consecutive numbers
- Express an odd number as sum of prime numbers
- Count ways to express even number ‘n’ as sum of even integers
- Count ways to express a number as sum of exactly two numbers
- Minimum number of single digit primes required whose sum is equal to N
- Minimum number of operations required to sum to binary string S
- Minimum count of numbers required ending with 7 to sum as a given number
- Minimum number of primes required such that their sum is equal to N
- Minimum number operations required to convert n to m | Set-2
- Minimum number of given powers of 2 required to represent a number
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.