Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Algorithms  >  Greedy Algorithm to Find Minimum Number of Coins

Greedy Algorithm to Find Minimum Number of Coins | Algorithms - Computer Science Engineering (CSE) PDF Download

Greedy Algorithm to find Minimum number of Coins

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.

Algorithm

  1. Sort the array of coins in decreasing order.
  2. Initialize result as empty.
  3. Find the largest denomination that is smaller than current amount.
  4. Add found denomination to result. Subtract value of found denomination from amount.
  5. If amount becomes 0, then print result.
  6. Else repeat steps 3 and 4 for new value of V.
  • C++
    Greedy Algorithm to Find Minimum Number of Coins | Algorithms - Computer Science Engineering (CSE)
  • C
    Greedy Algorithm to Find Minimum Number of Coins | Algorithms - Computer Science Engineering (CSE)
  • Java
    Greedy Algorithm to Find Minimum Number of Coins | Algorithms - Computer Science Engineering (CSE)
  • Python3
    Greedy Algorithm to Find Minimum Number of Coins | Algorithms - Computer Science Engineering (CSE)
  • C#
    Greedy Algorithm to Find Minimum Number of Coins | Algorithms - Computer Science Engineering (CSE)

Output:  
Following is minimal number of change
for 93: 50  20  20  2  1

Complexity Analysis 

  • Time Complexity: O(V).
  • Auxiliary Space: O(1) as no additional space is used.

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.

The document Greedy Algorithm to Find Minimum Number of Coins | Algorithms - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Algorithms.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
81 videos|80 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Greedy Algorithm to Find Minimum Number of Coins - Algorithms - Computer Science Engineering (CSE)

1. What is a Greedy Algorithm?
Ans. A greedy algorithm is an algorithmic paradigm that follows the problem-solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. In other words, it chooses the best option at each step without considering the overall consequences.
2. How does a Greedy Algorithm find the minimum number of coins?
Ans. To find the minimum number of coins using a greedy algorithm, we follow a simple approach. We start with the largest denomination and keep subtracting it from the total amount until we cannot subtract it anymore. Then, we move to the next smaller denomination and repeat the process until the total amount becomes zero. This approach ensures that we use the minimum number of coins to make the total amount.
3. What is the time complexity of the Greedy Algorithm for finding the minimum number of coins?
Ans. The time complexity of the greedy algorithm for finding the minimum number of coins depends on the number of denominations available. If there are n denominations, the time complexity is O(n), as we need to iterate through each denomination once.
4. Can the Greedy Algorithm always find the minimum number of coins?
Ans. No, the greedy algorithm may not always find the minimum number of coins. It depends on the denominations available and the total amount to be made. If the available denominations do not have a common factor or if the total amount cannot be made using the available denominations, the greedy algorithm may not give the optimal solution.
5. Are there any other algorithms to find the minimum number of coins apart from the Greedy Algorithm?
Ans. Yes, apart from the greedy algorithm, there are other algorithms like dynamic programming that can be used to find the minimum number of coins. Dynamic programming can handle cases where the greedy algorithm fails, such as when the available denominations do not have a common factor or when the total amount cannot be made using the available denominations. However, dynamic programming may have a higher time complexity compared to the greedy algorithm.
81 videos|80 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

Greedy Algorithm to Find Minimum Number of Coins | Algorithms - Computer Science Engineering (CSE)

,

shortcuts and tricks

,

Sample Paper

,

Extra Questions

,

Greedy Algorithm to Find Minimum Number of Coins | Algorithms - Computer Science Engineering (CSE)

,

Semester Notes

,

Viva Questions

,

Important questions

,

Summary

,

study material

,

MCQs

,

past year papers

,

Free

,

Exam

,

ppt

,

Greedy Algorithm to Find Minimum Number of Coins | Algorithms - Computer Science Engineering (CSE)

,

Objective type Questions

,

practice quizzes

,

video lectures

,

pdf

,

Previous Year Questions with Solutions

;