The Wayback Machine - https://web.archive.org/web/20240624050022/https://www.geeksforgeeks.org/buy-maximum-stocks-stocks-can-bought-th-day/
Open In App

Buy Maximum Stocks if i stocks can be bought on i-th day

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++





Java





Python3





C#





Javascript





Output

4

Time Complexity: O(nlogn).
Auxiliary Space: O(n)

 



Previous Article
Next Article

Similar Reads

Maximum litres of water that can be bought with N Rupees
Given [Tex]N [/Tex]Rupees. A liter plastic bottle of water costs [Tex]A [/Tex]Rupees and a litre of glass bottle of water costs [Tex]B [/Tex]Rupees. But the empty glass bottle after buying can be exchanged for [Tex]C [/Tex]Rupees. Find the maximum liters of water which can be bought with [Tex]N [/Tex]Rupees. Examples: Input: N = 10 , A = 11 , B = 9
5 min read
Maximum number of candies that can be bought
Given an array arr[] of size n where arr[i] is the number of candies of type i. You have an unlimited amount of money. The task is to buy as many candies as possible satisfying the following conditions: If you buy x(i) candies of type i (clearly, 0 ? x(i) ? arr[i]), then for all j (1 ? j ? i) at least one of the following must hold: x(j) < x(i)
6 min read
Maximum items that can be bought with the given type of coins
Given three integers X, Y and Z which represent the number of coins to buy some items. The cost of items is given below: Item typeCost13 X coins23 Y coins33 Z coins41 X coin + 1 Y coin + 1 Z coin The task is to find the maximum number of items that can be bought with the given number of coins. Input: X = 4, Y = 5, Z = 6 Output: 4 Buy 1 item of type
6 min read
Maximum items that can be bought from the cost Array based on given conditions
Given an array arr[] of size N where every index in the array represents the cost of buying an item and two numbers P, K. The task is to find the maximum number of items which can be bought such that: If some i-th object is bought from the array, the remaining amount becomes P - arr[i].We can buy K items, not necessarily consecutive, at a time by p
9 min read
Maximum number of mangoes that can be bought
Given two integers W and C, representing the number of watermelons and coins, the task is to find the maximum number of mangoes that can be bought given that each mango costs 1 watermelon and X coins and y coins can be earned selling a watermelon. Examples: Input: W = 10, C = 10, X = 1, Y = 1Output: 10Explanation: The most optimal way is to use 10
9 min read
Find the most expensive item that can not be bought
Given two distinct prime numbers, p1 and p2. Alice wants to buy a gift for Bob from a market that sells items priced at every positive integer. Alice has unlimited coins of denominations p1 and p2. The task is to find the highest price of an item that Alice cannot afford to buy for Bob. Example: Input: p1 = 2, p2 = 5Output: 3Explanation: The prices
9 min read
Maximize Profit by trading stocks based on given rate per day
Given an array arr[] of N positive integers which denotes the cost of selling and buying a stock on each of the N days. The task is to find the maximum profit that can be earned by buying a stock on or selling all previously bought stocks on a particular day.Examples: Input: arr[] = {2, 3, 5} Output: 5 Explanation: Price on Day 1 = 2. Therefore buy
6 min read
Find the first day of a given year from a base year having first day as Monday
Given two integers Y and B representing two years, the task is to find the day of the week in which 1st January of the year Y lies assuming that 1st January of the year B was a Monday. Examples: Input:Y = 2020B = 1900Output:WednesdayExplanation:01/01/2020 was a Wednesday considering that 01/01/1900 was a Monday Input:Y = 2020B = 1905Output:Thursday
6 min read
Find the day of the week after K days from the given day
Content has been removed on Author’s request. Using Naive Approach:Approach: One approach to find the day of the week after K days from a given day is to use a brute-force method where we add K days to the given day and then find the corresponding day of the week. Algorithm: Input the day and KAdd K days to the given dayFind the corresponding day o
3 min read
Find the minimum and maximum amount to buy all N candies
In a candy store, there are N different types of candies available and the prices of all the N different types of candies are provided. There is also an attractive offer by the candy store. We can buy a single candy from the store and get at most K other candies (all are different types) for free. Find the minimum amount of money we have to spend t
11 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg