Given an array of daily stock prices, find the maximum possible return on investment. The return on investment is calculated as the difference between the selling price and the buying price.
Explanation: Step-by-step: The maximum return on investment is achieved by buying on day 1 (price 1) and selling on day 5 (price 6), giving a return of 5.
Explanation: Step-by-step: The maximum return on investment is achieved by buying on day 2 (price 1) and selling on day 3 (price 4), giving a return of 3. However, the maximum return is 2, achieved by buying on day 2 (price 1) and selling on day 3 (price 4) is incorrect. The correct answer is achieved by buying on day 1 (price 2) and selling on day 3 (price 4), giving a return of 2.
Iterating arrays and tracking min/max
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak