Question: You re working on some more stock - prediction software. The function you re writing accepts an array of predicted prices for a particular stock

Youre working on some more stock-prediction software. The function youre writing accepts an array of predicted prices for a particular stock over the course of time.
For example, this array of seven prices:
[10,7,5,8,11,2,6]
predicts that a given stock will have these prices over the next seven days. (On Day 1, the stock will close at $10; on Day 2, the stock will close at $7; and so on.)
Your function should calculate the greatest profit that could be made from a single buy transaction followed by a single sell transaction.
In the previous example, the most money could be made if we bought the stock when it was worth $5 and sold it when it was worth $11. This yields a profit of $6 per share.
Note that we could make even more money if we buy and sell multiple times, but for now, this function focuses on the most profit that could be made from just one purchase followed by one sale.
Now, we could use nested loops to find the profit of every possible buy and sell combination. However, this would be O(N2) and too slow for our hotshot trading platform.
Your job is to optimize the code in C++ so that the function clocks in at just O(N).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!