- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Program to find number of distinct coin sums we can make with coins and quantities in Python?

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

## Example

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))

## Input

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

## Output

10

- Related Articles
- 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 maximum coins we can get from disappearing coins matrix 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 maximum number of people we can make happy 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 distinct subsequences in Python
- Program to count number of strings we can make using grammar rules in Python
- C++ program to find number of distinct values we can get after negating a subarray
- Program to find k sublists with largest sums and return sums in ascending order in Python

Advertisements