Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use recursion please Given the forecasted price of a stock over time, write a divide and conquer algorithm that determines the best time to purchase

Use recursion please image text in transcribed
Given the forecasted price of a stock over time, write a divide and conquer algorithm that determines the best time to purchase and sell the stock (in order to maximize profit). It is assumed that a profit can be made (based on the forecast). It is assumed that the forecasted price of the stock over time is represented by a vector of integers (for simplicity, we are assuming the stock price is integer-valued). Thus, the goal is to find two indices x and y (where 0 maxProfit (const vector & s, int start, int end) The return value is a vector of size 3. The first element is the maximum profit S[y] - S[x], the second element is the purchase time x, and the third element is the sell time y. Test Case #1: S = {25, 1, 2, 11, 4, 8, 6,10) Maximum profit is S[3] - S[1] -11 -1 = 10. The algorithm should return the following vector: {10, 1, 3} Test Case #2: S = {17, 4, 8, 6,10, 1, 2, 14) Test Case #3: S= {1, 2, 4, 8, 6, 12,10)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Bachelors degree in Information Systems or Statistics

Answered: 1 week ago