- 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

# Python program to find ways to get n rupees with given coins

Suppose we have given a coins of denominations (1, 2, 5 and 10). We have to find in how many ways can we can arrange n using these dominations. We have an array called count with 4 elements, where count[0] indicates number of coins of 1, count[1] indicates number of coins for 2 and so on.

So, if the input is like n = 27 count = [8,4,3,2], then the output will be 18 so 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 + 4*2 + 4*1 = 27

and so on...

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

denom = [1,2,5,10] def solve(n, count): 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] n = 27 count = [8,4,3,2] print(solve(n, count))

## Input

27, [8,4,3,2]

## Output

18

- Related Articles
- Program to find number of coins needed to make the changes with given set of coins in Python
- Program to find maximum coins we can get from disappearing coins matrix in Python
- Program to find maximum number of coins we can get using Python
- Program to find number of ways we can get n R.s using Indian denominations in Python
- Program to count number of ways we can distribute coins to workers in Python
- C++ program to check the coins forms x amount of rupees or not
- I have a total of rupees 300 in coins of denomination rupees 1, 2, 5 the number of rupees 2 coins is 3 times the number of rupees 5 coins. The total number of coins is 160. Find the number of coins of each denomination.
- Program to find number of ways we can arrange symbols to get target in Python?
- Python Program to get K length groups with given summation
- Program to find last digit of the given sequence for given n in Python
- Program to find number of ways to form a target string given a dictionary in Python
- Program to find number of combinations of coins to reach target in Python
- Program to find number of coins needed to make the changes in Python
- C++ program to calculate how many years needed to get X rupees with 1% interest
- Program to find maximum number of coins we can collect in Python

Advertisements