In a stock market, there is a product with its infinite stocks. The stock prices are given for N days, where arr[i] denotes the price of the stock on the ith day. There is a rule that a customer can buy at most i stock on the ith day. If the customer has k amount of money initially, find out the maximum number of stocks a customer can buy.
For example, for 3 days the price of a stock is given as 7, 10, 4. You can buy 1 stock worth 7 rs on day 1, 2 stocks worth 10 rs each on day 2 and 3 stock worth 4 rs each on day 3.
Examples:
Input : price[] = { 10, 7, 19 },
k = 45.
Output : 4
A customer purchases 1 stock on day 1 for 10 rs,
2 stocks on day 2 for 7 rs each and 1 stock on day 3 for 19 rs.Therefore total of
10, 7 * 2 = 14 and 19 respectively. Hence,
total amount is 10 + 14 + 19 = 43 and number
of stocks purchased is 4.
Input : price[] = { 7, 10, 4 },
k = 100.
Output : 6
The idea is to use greedy approach, where we should start buying product from the day when the stock price is least and so on.
So, we will sort the pair of two values i.e { stock price, day } according to the stock price, and if stock prices are same, then we sort according to the day.
Now, we will traverse along the sorted list of pairs, and start buying as follows:
Let say, we have R rs remaining till now, and the cost of product on this day be C, and we can buy atmost L products on this day then,
total purchase on this day (P) = min(L, R/C)
Now, add this value to the answer
total_purchase = total_purchase + P, where P =min(L, R/C)
Now, subtract the cost of buying P items from remaining money, R = R – P*C.
Total number of products that we can buy is equal to total_purchase.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int buyMaximumProducts( int n, int k, int price[])
{
vector<pair< int , int > > v;
for ( int i = 0; i < n; ++i)
v.push_back(make_pair(price[i], i + 1));
sort(v.begin(), v.end());
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;
}
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;
}
|
Java
import java.util.*;
public class GFG {
static int buyMaximumProducts( int [] price, int K, int n)
{
Pair[] arr = new Pair[n];
for ( int i = 0 ; i < n; i++)
arr[i] = new Pair(price[i], i + 1 );
Arrays.sort(arr, new SortPair());
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
ans += Math.min(arr[i].second,
K / arr[i].first);
K -= arr[i].first
* Math.min(arr[i].second,
K / arr[i].first);
}
return ans;
}
public static void main(String[] args)
{
int [] price = { 10 , 7 , 19 };
int K = 45 ;
System.out.println(
buyMaximumProducts(price, K, price.length));
}
}
class Pair {
int first, second;
Pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
class SortPair implements Comparator<Pair> {
public int compare(Pair a, Pair b)
{
return a.first - b.first;
}
}
|
Python3
def buyMaximumProducts(n, k, price):
arr = []
for i in range (n):
arr.append([i + 1 , price[i]])
arr.sort(key = lambda x: x[ 1 ])
total_purchase = 0
for i in range (n):
P = min (arr[i][ 0 ], k / / arr[i][ 1 ])
total_purchase + = P
k - = (P * arr[i][ 1 ])
return total_purchase
price = [ 10 , 7 , 19 ]
n = len (price)
k = 45
print (buyMaximumProducts(n, k, price))
|
C#
using System;
class GFG
{
static void Main( string [] args)
{
int [] price = { 10, 7, 19 };
int K = 45;
Console.WriteLine(
buyMaximumProducts(price, K, price.Length));
}
static int buyMaximumProducts( int [] price, int K, int n)
{
Pair[] arr = new Pair[n];
for ( int i = 0; i < n; i++)
arr[i] = new Pair(price[i], i + 1);
Array.Sort(arr);
int ans = 0;
for ( int i = 0; i < n; i++) {
ans += Math.Min(arr[i].second,
K / arr[i].first);
K -= arr[i].first
* Math.Min(arr[i].second,
K / arr[i].first);
}
return ans;
}
}
class Pair : IComparable<Pair> {
public int first, second;
public Pair( int first, int second)
{
this .first = first;
this .second = second;
}
public int CompareTo(Pair other)
{
return this .first - other.first;
}
}
|
Javascript
function buyMaximumProducts(n, k, price) {
let v = [];
for (let i = 0; i < n; ++i) {
v.push([price[i], i + 1]);
}
v.sort((a, b) => a[0] - b[0]);
let ans = 0;
for (let i = 0; i < n; ++i) {
ans += Math.min(v[i][1], Math.floor(k / v[i][0]));
k -= v[i][0] * Math.min(v[i][1], Math.floor(k / v[i][0]));
}
return ans;
}
let price = [10, 7, 19];
let n = price.length;
let k = 45;
console.log(buyMaximumProducts(n, k, price));
|
Time Complexity: O(nlogn).
Auxiliary Space: O(n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!