- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of values called coins and another list called quantities of the same length. The value of ith coin is coins[i] and we currently have quantities[i] number of ith coin. We have to find number of distinct coin sum values we can get by using non-empty group of these coins.

So, if the input is like coins = [1, 2, 5] quantities = [1, 2, 1], then the output will be 10, as we can have the following distinct coin sums [1] = 1, [2] = 2, [1,2] = 3, [2,2] = 4, [5] = 5,[1,5] = 6, [2,5] = 7, [1,2,5] = 8, [2,2,5] = 9, [1,2,2,5] = 10.

To solve this, we will follow these steps:

Define a function rec() . This will take i, res

if i is same as size of coins , then return for k in range 0 to quantities[i] + 1, do cur := res + k * coins[i] insert cur into fres rec(i + 1, cur) From the main method, do the following: fres := a new set rec(0, 0) return size of fres - 1

class Solution: def solve(self, coins, quantities): def rec(i, res): if i == len(coins): return for k in range(0, quantities[i] + 1): cur = res + k * coins[i] fres.add(cur) rec(i + 1, cur) fres = set() rec(0, 0) return len(fres) - 1 ob = Solution() coins = [1, 2, 5] quantities = [1, 2, 1] print(ob.solve(coins, quantities))

[1, 2, 5], [1, 2, 1]

10

- Related Questions & Answers
- Program to find maximum number of coins we can collect in Python
- Program to find maximum number of coins we can get using Python
- Program to find number of coins needed to make the changes with given set of coins in Python
- Program to find number of coins needed to make the changes in Python
- Program to find number of coins we can pick from topleft to bottom-right cell and return in Python
- Program to count number of ways we can distribute coins to workers in Python
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find possible number of palindromes we can make by trimming string in Python
- Program to find maximum amount of coin we can collect from a given matrix in Python
- Program to find number of combinations of coins to reach target in Python
- Program to find k sublists with largest sums and return sums in ascending order in Python
- Program to count number of strings we can make using grammar rules in Python
- Program to find maximum profit we can make by buying and selling stocks in Python?
- Program to find maximum profit we can make after k Buy and Sell in python
- Program to make pairwise adjacent sums small in Python

Advertisements