Skip to content

Commit

Permalink
Merge pull request #783 from frostop1/sd3
Browse files Browse the repository at this point in the history
Created Solution program to find maximum number of stocks that  can be bought with given constraints.
  • Loading branch information
tanus786 authored Oct 29, 2022
2 parents df0255d + 2cb8e0d commit 336e4f9
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Advanced data structures/Maximum Stocks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// C++ program to find maximum number of stocks that
// can be bought with given constraints.
#include <bits/stdc++.h>
using namespace std;

// Return the maximum stocks
int buyMaximumProducts(int n, int k, int price[])
{
vector<pair<int, int> > v;

// Making pair of product cost and number
// of day..
for (int i = 0; i < n; ++i)
v.push_back(make_pair(price[i], i + 1));

// Sorting the vector pair.
sort(v.begin(), v.end());

// Calculating the maximum number of stock
// count.
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += min(v[i].second, k / v[i].first);
k -= v[i].first * min(v[i].second,
(k / v[i].first));
}

return ans;
}

// Driven Program
int main()
{
int price[] = { 10, 7, 19 };
int n = sizeof(price)/sizeof(price[0]);
int k = 45;

cout << buyMaximumProducts(n, k, price) << endl;

return 0;
}

0 comments on commit 336e4f9

Please sign in to comment.