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
Python program to find ways to get n rupees with given coins
Suppose we have coins of denominations (1, 2, 5 and 10). We need to find in how many ways we can make n rupees using these denominations. We have an array called count with 4 elements, where count[0] indicates number of coins of denomination 1, count[1] indicates number of coins of denomination 2, and so on.
So, if the input is like n = 27 and count = [8,4,3,2], then the output will be 18, meaning there are 18 possible combinations. Some of them are ?
10*2 + 5*1 + 2*1 = 27
10*2 + 2*3 + 1*1 = 27
10*1 + 5*3 + 2*1 = 27
10*1 + 5*1 + 2*4 + 1*4 = 27
and so on...
Algorithm
To solve this, we will follow these steps ?
- denom := [1,2,5,10]
- A := an array of size (n + 1) and fill with 0
- B := a new list from A
- for i in range 0 to (minimum of count[0] and n), do
- A[i] := 1
- for i in range 1 to 3, do
- for j in range 0 to count[i], do
- for k in range 0 to n + 1 - j *denom[i], do
- B[k + j * denom[i]] := B[k + j * denom[i]] + A[k]
- for k in range 0 to n + 1 - j *denom[i], do
- for j in range 0 to n, do
- A[j] := B[j]
- B[j] := 0
- for j in range 0 to count[i], do
- return A[n]
Example
Let us see the following implementation to get better understanding ?
def solve(n, count):
denom = [1, 2, 5, 10]
A = [0 for _ in range(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]
# Test with the given example
n = 27
count = [8, 4, 3, 2]
result = solve(n, count)
print(f"Number of ways to make {n} rupees: {result}")
The output of the above code is ?
Number of ways to make 27 rupees: 18
How It Works
The algorithm uses dynamic programming with the following approach ?
- Initialization: Array A stores the number of ways to make each amount from 0 to n
- Base case: For denomination 1, we can make amounts 0 to min(count[0], n) in exactly 1 way each
- Iteration: For each higher denomination (2, 5, 10), we calculate how many additional ways we can form each amount
- Update: Array B temporarily stores new combinations, then updates A for the next iteration
Step-by-Step Example
For n = 27 with count = [8, 4, 3, 2] ?
def solve_with_steps(n, count):
denom = [1, 2, 5, 10]
A = [0 for _ in range(n + 1)]
B = list(A)
# Step 1: Initialize for denomination 1
for i in range(min(count[0], n) + 1):
A[i] = 1
print(f"After processing denomination 1: A[0:10] = {A[0:10]}")
# Step 2: Process other denominations
for i in range(1, 4):
print(f"\nProcessing denomination {denom[i]} with {count[i]} coins")
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
print(f"Ways to make amount 27: {A[27]}")
return A[n]
result = solve_with_steps(27, [8, 4, 3, 2])
After processing denomination 1: A[0:10] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] Processing denomination 2 with 4 coins Ways to make amount 27: 5 Processing denomination 5 with 3 coins Ways to make amount 27: 13 Processing denomination 10 with 2 coins Ways to make amount 27: 18
Conclusion
This dynamic programming solution efficiently calculates the number of ways to make change using limited coins of each denomination. The algorithm has a time complexity of O(n * sum of coin counts) and provides an optimal solution for the coin change counting problem.
