Suppose we have limited coins of denominations (₹1, ₹2, ₹5 and ₹10). We have to find in how many ways can you sum them up to a total of ₹n? We have an array count of size 4, where count[0] indicates coins of ₹1, count[1] indicates coins of ₹2 and so on.
So, if the input is like n = 25 count = [7,3,2,2], then the output will be 9.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
denom = [1,2,5,10] def solve(n, count): A = [0] * (n + 1) B = list(A) for i in range(min(count[0], n) + 1): A[i] = 1 for i in range(1, 4): for j in range(0, count[i] + 1): for k in range(n + 1 - j *denom[i]): B[k + j * denom[i]] += A[k] for j in range(0, n + 1): A[j] = B[j] B[j] = 0 return A[n] n = 25 count = [7,3,2,2] print(solve(n, count))
25, [7,3,2,2]
9