FastPrepFastPrep
Problem Brief

Selecting Stocks

INTERNOA

An investor has saved some money and wants to invest in the stock market. There are a number of stocks to choose from, and they want to buy at most 1 share in any company. The total invested cannot exceed the funds available. A friend who is a stock market expert has predicted the values of each stock after 1 year. Determine the maximum profit that can be earned at the end of the year assuming the predictions come true.

Function Description

Complete the function selectStock in the editor below. The function should return an integer that denotes the maximum profit after one year.

selectStock has the following parameter(s):

  1. 1. saving: amount available for investment
  2. 2. int currentValue[n]: the current stock values
  3. 3. int futureValue[n]: the values of the stocks after one year

1Example 1

Input
saving = 250, currentValue = [175, 133, 109, 210, 97], futureValue = [200, 125, 128, 228, 133]
Output
55
Explanation
To maximize profits, the investor should buy stocks at indices 2 and 4 for an investment of 109 + 97 = 206. At the end of the year the stocks are sold for 128 + 133 = 261, so total profit is 261 - 206 = 55.

Constraints

Limits and guarantees your solution can rely on.

๐ŸŽ๐ŸŽ
public int selectStock(int saving, int[] currentValue, int[] futureValue) {
  // write your code here
}
Input

saving

250

currentValue

[175, 133, 109, 210, 97]

futureValue

[200, 125, 128, 228, 133]

Output

55

Sign in to submit your solution.