Given a value V, if we want to make a change for V Rs, and we have an infinite supply of each of the denominations in Indian currency, i.e., we have an infinite supply of { 1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes, what is the minimum number of coins and/or notes needed to make the change?
Examples:
Input: V = 70
Output: 2
We need a 50 Rs note and a 20 Rs note.
Input: V = 121
Output: 3
We need a 100 Rs note, a 20 Rs note and a 1 Rs coin.
Solution: Greedy Approach.
Approach: A common intuition would be to take coins with greater value first. This can reduce the total number of coins needed. Start from the largest possible denomination and keep adding denominations while the remaining value is greater than 0.
Output:
Following is minimal number of change
for 93: 50 20 20 2 1
Complexity Analysis
Note: The above approach may not work for all denominations. For example, it doesn’t work for denominations {9, 6, 5, 1} and V = 11. The above approach would print 9, 1 and 1. But we can use 2 denominations 5 and 6.
81 videos|80 docs|33 tests
|
1. What is a Greedy Algorithm? |
2. How does a Greedy Algorithm find the minimum number of coins? |
3. What is the time complexity of the Greedy Algorithm for finding the minimum number of coins? |
4. Can the Greedy Algorithm always find the minimum number of coins? |
5. Are there any other algorithms to find the minimum number of coins apart from the Greedy Algorithm? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|