Stock Selling Problem

is below two transaction consider overlapping or not ??

for t = 1
k = 2
n = 5
arr[] = 23 4 5 9 20

can i sell 2nd day stock arr[1] , 3rd day stock arr[2] on 5th day arr[4]
to gain maximum profit.
total profit = 31.

is above two transaction consider overlapping or not ??

note : in above array is zero based indexing

Hi @rajujnvgupta

By overlapping, it means that you can buy again only when you have sold the previous ones.
buy-sell-buy-sell is valid
buy-buy-sell is invalid

In the given Case, it is best to buy on 2nd day(price=4) and sell on day 5(price=20) giving profit as 16. Please see only 1 transaction is allowed.

what actually asking is
let total transaction = k = 2;
for 1st transaction buying on 2nd day and selling 5th day
for 2nd transaction buying on 3rd day and selling on 5th day

is considered overlapping or not ??

what will be output for following test case
sample input

1
2 5
23 4 5 9 20

Yes,it would be considered as overlapping.
Consider it day wise:
Buy-Buy-Sell-None-Sell is what you want but isn’t allowed.
Buy a stock, sell it, then only you are allowed to buy again.

For this sample, Buy at 4 and sell at 20 maximizes the profit.
Other possibility (buy at 4, sell at 5, buy at 9, sell at 20) gives profit 12 which is less than 20.

I hope you are clear with the understanding of the question.
Please mark the thread Resolved if so :slight_smile:

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.