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
11 is itself a palindrome.
Input: N = 65
65 can be expressed as a sum of three palindromes (55, 9, 1).
We can use Dynamic Programming to solve this problem. The idea is to first generate all the palindromes up to N in a sorted fashion, and then we can treat this problem as a variation of the subset sum problem, where we have to find the size of the smallest subset such that its sum is N.
Below is the implementation of above approach:
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.