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 maximum 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
- Minimum number of swaps required to make a number divisible by 60
- Minimum number of given powers of 2 required to represent a number
- Minimum number of integers required such that each Segment contains at least one of them
- Minimum number of changes required to make the given array an AP
- Minimum number operations required to convert n to m | Set-2
- Minimum number of primes required such that their sum is equal to N
- Minimum number of operations required to reduce N to 1
- Minimum number of given operation required to convert n to m
- Minimum number of operations required to sum to binary string S
- Minimum number of integers required to fill the NxM grid
- Minimum number of bottles required to fill K glasses
- Minimum number of given moves required to make N divisible by 25
- Minimum number of mails required to distribute all the questions
- Minimum count of numbers required ending with 7 to sum as a given number
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of operations required to obtain a given Binary String
- Find out the minimum number of coins required to pay total amount
- Minimum number of points required to cover all blocks of a 2-D grid
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.