diff --git a/algorithms/greedy/minimumCoins.cpp b/algorithms/greedy/minimumCoins.cpp new file mode 100644 index 00000000..e231d251 --- /dev/null +++ b/algorithms/greedy/minimumCoins.cpp @@ -0,0 +1,41 @@ +// Given a value V and the list of available denomination of money, +// find minimum number of coins and/or notes needed to make the change. + +#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]); + +vector calculate(int V) +{ + sort(deno, deno + n); + vector ans; + + for (int i = n - 1; i >= 0; i--) { + + while (V >= deno[i]) { + V -= deno[i]; + ans.push_back(deno[i]); + } + } + return ans; + //for (int i = 0; i < ans.size(); i++) + //cout << ans[i] << " "; +} + +int main() +{ + int n; + cout<<"Enter the monitory value: "; + cin>>n; + cout << "Following is minimal number of change for " << n + << ": "; + vector ans = calculate(n); + for(auto i: ans) + cout< +using namespace std; + +int calculate(int a[], int n) +{ + if (n == 1) + return a[0]; + + int max_neg = INT_MIN; + int min_pos = INT_MAX; + int count_neg = 0, count_zero = 0; + int prod = 1; + for (int i = 0; i < n; i++) { + + + if (a[i] == 0) { + count_zero++; + continue; + } + + if (a[i] < 0) { + count_neg++; + max_neg = max(max_neg, a[i]); + } + + if (a[i] > 0) + min_pos = min(min_pos, a[i]); + + prod = prod * a[i]; + } + + if (count_zero == n || + (count_neg == 0 && count_zero > 0)) + return 0; + + if (count_neg == 0) + return min_pos; + + if (!(count_neg & 1) && count_neg != 0) { + prod = prod / max_neg; + } + + return prod; +} + +int main() +{ + int n; + cout<<"Enter size of array: "; + cin>>n; + int a[n]; + cout<<"\nEnter array elements: "; + for(int i=0;i>a[i]; + cout << "\nAnswer: "<