Doubt in stock selling?

how can the sample test case ans will be 16?

The array is {23 4 5 9 20} and the value of K is 1. Hence you can only buy and sell at most once. Since you need 1 transaction, hence the best way is to buy a stock on day 2, which will cost you Rs 4 and sell it on the last day which will give you Rs 20, hence the total profit earned is selling price - cost price which is 20 - 4 = 16. All other combinations will not yield you more than Rs 16.

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.