Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions algorithms/greedy/minimumCoins.cpp
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>
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<int> calculate(int V)
{
sort(deno, deno + n);
vector<int> 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<int> ans = calculate(n);
for(auto i: ans)
cout<<i<<" ";
cout<<"\nMinimum Denominations required: "<<ans.size();
return 0;
}
59 changes: 59 additions & 0 deletions algorithms/greedy/minimumProductSubset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//Find minimum product subset in an array using Greedy method

#include <bits/stdc++.h>
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<n;i++)
cin>>a[i];
cout << "\nAnswer: "<<calculate(a, n);
return 0;
}