Question

You are working for a small stock investment company that wants to look for patterns in...

You are working for a small stock investment company that wants to look for patterns in optimal trading days in a given time period of n days. They want to find the best pair of days in a period of n days to buy a stock on the first day of the pair and sell it on the second day of the pair. That is, they want the biggest positive difference between the selling price on the second day and the buying price on the first day. Assume for simplicity that the buying and selling price on a given day are the same. Assume you know the stock price for every day. The data is given to you in a list sorted by date, earliest day first. You may assume that there exists at least one trade that results in a profit. Specify an O(n log n) Divide and Conquer algorithm. (Note: there are O (n) algorithms that solve this problem but that is not what is asked for here)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We need to find 2 date indexes (left and right) between which profit is maximum.

In divide and conquer approach,

1.we need to divide problem in two parts i.e left half and right half.

the final result will be in one of the three possiblities i.e

1. Left half of the array ( both buy and sell date in left half of the array).

2. Right half of the array ( both buy and sell date in right half of the array).

3. across both side (Buy date in the left half and sell date in right half of the array).

2. Solve the all three and return the maximum among them.

3. Left half and right half of the array will be solved recursively

4. keep track of profit and date indexes.

Algo-

maxprofit (prices, start, end){

mid = start + (end-start)/2;

left_ans= maxProfit(prices,start,mid)

right_ans= maxProfit(prices,mid+1,end)

minLeftIndex = getMinIndex(prices, start, mid) which returns minimum value index in array.

maxRightIndex = getMaxIndex(prices, mid, end) which returns maximum value index in array.

crossProfit = prices[maxRightIndex] - prices[minLeftIndex];

   if(crossProfit>left_ans.profit && crossProfit>right_ans.profit)

              return new ans(crossProfit,minLeftIndex,maxRightIndex);

    else if(left_ans.profit>crossProfit && right_ans.profit>crossProfit)

             return left_ans

     else

            return right_ans

}

feel free to ask any doubt in comments

Add a comment
Know the answer?
Add Answer to:
You are working for a small stock investment company that wants to look for patterns in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (5 +2 points) You are given an array A[1..n] of positive numbers where Ai] is the stock price on ...

    (5 +2 points) You are given an array A[1..n] of positive numbers where Ai] is the stock price on day i. You are allowed to buy the stock once and sell it at some point later. For each day you own the stock you pay S1 fee. Design a divide-and-conquer algorithm that will return a pair (i,j) such that buying the stock on day i and selling it on day j will maximize your gain The complexity of the algorithm...

  • Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct...

    Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-i. Give a divide-and-conquer algorithm that runs in time O(log n)

  • Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] ·...

    Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] · · · X[n] and Y [1] · · · Y [n] of integers. For simplicity, assume that n is a power of 2. Problem is to design an algorithm that determines if there is a number p in X and a number q in Y such that p + q is zero. If such numbers exist, the algorithm returns true; otherwise, it returns false....

  • Problem 7. Stock Market For this program you are given a String that represents stock prices...

    Problem 7. Stock Market For this program you are given a String that represents stock prices for a particular com- y over a period of time. For example, you may be given a String that looks like the following: String stockPrices = "1,22,30,38,44,68,49,73,52,66"; Each number in this String represents the stock price for a particular day. The first number is day 0, the second number is day 1, and so on. You can al supplied string will formatted using commas...

  • How much would you pay for a share of stock paying a dividend (cash payout C)...

    How much would you pay for a share of stock paying a dividend (cash payout C) of $4 to be paid in one year, a known selling price in one year (P) of $50, and expected return (R) of similar assets of 4%? You would pay $ (Round your response to the nearest penny) Consider the one-period valuation model for computing stock prices. Suppose you are considering buying Wal-Mart stock, which has a price of $75.06 and a dividend of...

  • Stat 255 Project 3 due Wednesday, April 22 Write R code to solve the following problems, Make sur...

    I am just wanting the first question answered. Stat 255 Project 3 due Wednesday, April 22 Write R code to solve the following problems, Make sure to include descriptions and explanation in your cod Save them in a file named project3-yourname.R and email them to ysarolousi.edu be date. A model for stock prices Let S, be the closing price of a stock at the end of day j, where j model for the evolution of the future daily closing prices:...

  • I have to make stock changes for the 90 days and professor showed example for 50...

    I have to make stock changes for the 90 days and professor showed example for 50 days. I do not know how to do it. The price of a share of a particular stock listed on the New York Stock Exchange is currently $39. The following probability distribution shows how the price per share is expected to change over a three-month period: Stock Price Change ($) - Probability 0.05 0.10 0.25 0.20 0.20 0.10 0.10 + + +1 +2 +3...

  • 82% Assume that you are considering purchasing stock as an investment. You have narrowed the choice to either Power Corporation stock or E-shop Company stock and have assembled the...

    82% Assume that you are considering purchasing stock as an investment. You have narrowed the choice to either Power Corporation stock or E-shop Company stock and have assembled the following data for the two companies. EEB (Click the icon to view the income statement data.) B(Click the icon to view data at end of current year) 圈(Click the icon to view data at beginning of current year) Your strategy is to invest in companies that have low price-earnings ratios but...

  • Please do this in excel and show work. DEF Problem - You have found three investment...

    Please do this in excel and show work. DEF Problem - You have found three investment choices for a one-year deponit: 10% APR compounded monthly, 10% APR compounded annually, and 9% APR compounded daily. Compute the EAR for each investment choice. (Asume that there are 365 days in the year.) Complete the steps below using cell references to given data or previous calculations. In some cases, a simple cell reference is all you need. To copy paste a formula across...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT