Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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] and 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.
Algorithm
To solve this, we will follow these steps ?
- Define a recursive function
rec()that takes current coin index and current sum - For each coin, try using 0 to maximum available quantity
- Store all possible sums in a set to avoid duplicates
- Return the count of distinct non-zero sums
Pseudocode
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:
fres := a new set
rec(0, 0)
return size of fres - 1
Implementation
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
# Test the solution
ob = Solution()
coins = [1, 2, 5]
quantities = [1, 2, 1]
result = ob.solve(coins, quantities)
print(f"Distinct coin sums: {result}")
Distinct coin sums: 10
How It Works
The recursive function explores all possible combinations by:
- Starting from the first coin (index 0) with sum 0
- For each coin, trying all quantities from 0 to maximum available
- Adding the current sum to the set at each step
- Recursively processing the next coin
The set automatically handles duplicates, ensuring we count only distinct sums. We subtract 1 from the final count to exclude the empty sum (0).
Alternative Dynamic Programming Approach
def count_distinct_sums(coins, quantities):
possible_sums = {0}
for i in range(len(coins)):
new_sums = set()
for existing_sum in possible_sums:
for k in range(quantities[i] + 1):
new_sum = existing_sum + k * coins[i]
new_sums.add(new_sum)
possible_sums = new_sums
return len(possible_sums) - 1 # Exclude sum = 0
# Test the alternative approach
coins = [1, 2, 5]
quantities = [1, 2, 1]
result = count_distinct_sums(coins, quantities)
print(f"Distinct coin sums: {result}")
Distinct coin sums: 10
Conclusion
Both recursive and dynamic programming approaches solve the coin sum problem by exploring all possible combinations. The DP approach is more efficient as it avoids redundant recursive calls by building solutions iteratively.
