BackmediumArrays Amazon Uber

Optimizing Portfolio Returns Solution

Problem Statement

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.

Example 1
Input
[7,1,5,3,6,4]
Output
5

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.

Example 2
Input
[2,4,1]
Output
2

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.

Constraints

  • The input array will have at least 2 elements.
  • The input array will contain only positive integers.
  • The selling price must be greater than the buying price.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free