From 0cf16ef93bf29e6516c3cf9761a8cb38e678410b Mon Sep 17 00:00:00 2001 From: ARNAB GHOSH <66074296+ghosharnab969@users.noreply.github.com> Date: Sat, 30 Oct 2021 21:13:03 +0530 Subject: [PATCH] Create Greedy -Algorithm_Finding-minimum-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? I have solved the above question using the Greedy Algorithm. --- cpp/Greedy -Algorithm_Finding-minimum-coins | 41 +++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 cpp/Greedy -Algorithm_Finding-minimum-coins diff --git a/cpp/Greedy -Algorithm_Finding-minimum-coins b/cpp/Greedy -Algorithm_Finding-minimum-coins new file mode 100644 index 0000000..bf74ec2 --- /dev/null +++ b/cpp/Greedy -Algorithm_Finding-minimum-coins @@ -0,0 +1,41 @@ + +#include +using namespace std; + +// All denominations of Indian Currency +int deno[] = { 1, 2, 5, 10, 20, + 50, 100, 500, 1000 }; +int n = sizeof(deno) / sizeof(deno[0]); + +void findMin(int V) +{ + sort(deno, deno + n); + + // Initialize result + vector ans; + + // Traverse through all denomination + for (int i = n - 1; i >= 0; i--) { + + // Find denominations + while (V >= deno[i]) { + V -= deno[i]; + ans.push_back(deno[i]); + } + } + + // Print result + for (int i = 0; i < ans.size(); i++) + cout << ans[i] << " "; +} + +// Driver program +int main() +{ + int n = 93; + cout << "Following is minimal" + << " number of change for " << n + << ": "; + findMin(n); + return 0; +}