Problem Link - Smallest Numbers of Notes Practice Problem in 500 to 1000 difficulty problems
Problem Statement:
Given a currency system with six denominations of notes: Rs. 1, Rs. 2, Rs. 5, Rs. 10, Rs. 50, Rs. 100, you need to find the smallest number of notes that combine to give a sum N for each test case.
Approach:
- Greedy Algorithm:
- The approach should be greedy because we want to use the highest denomination first to minimize the total number of notes.
- Start by trying to use as many Rs. 100 notes as possible, then Rs. 50 notes, and so on, until we reach Rs. 1.
- Steps:
- For each test case:
- Start with the largest denomination (Rs. 100) and divide
Nby the denomination to get the maximum number of notes for that denomination. - Subtract the equivalent amount from
Nand move to the next smaller denomination. - Repeat until
Nbecomes0.
- Start with the largest denomination (Rs. 100) and divide
- The number of notes used for each denomination should be added together to get the total number of notes for that test case.
- For each test case:
- Constraints:
- Since
Ncan be as large as1,000,000, the solution needs to be efficient. A greedy approach works in this case, as it guarantees an optimal solution.
- Since
Complexity:
- Time Complexity:
O(1)as the loop only runs six times because of the number of elements or currency note in the array. - Space Complexity:
O(1)No extra space is needed.