-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #783 from frostop1/sd3
Created Solution program to find maximum number of stocks that can be bought with given constraints.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |