Can someone help me with a function for this.
Also, what BIG O notation would the function be?
stock prices change from day to day.
we might have: {4,-1,-2,3,5,-7,1,0,0,-2,4}.
Which meant if we bought the stock after the second day, and held it for 3 days, the price would go down by 2, then up by 3, and then up by 5. If we sold it then, we would have made $6.
Design an O(N2) or better (it can actually be done in O(N) time) algorithm to find the days to buy and sell the stock that makes you the most profit (as well as the profit made). It is possible that every combination of days will lose you money, in which case you should not buy the stock at all and have a profit of $0.
answer:
If we are allowed to buy and sell once we use the following algorithm.
let us first define the algorithm in general sentences.
1) First we need to find the point where the value is least and take it as starting index.
2) Second, we need to find the point where the value is maxed and take it as ending index.
Algorithm:
step 1: Start
step 2: Decalre price array and n(length)
step 3: Read the value of the stock_price change and assign to price_arrayy and initialize n = length of the array and i=0,j=0
step 4: Initialize num_of_ways_buysell=0 and define sell_date and buy_date array
step 5: if n==1: return the value should be greater than one
step 6: while i should be less than n-1:
//find local minima which are our starting index:
while i< n-1 and stock_price[i+1] should be less than or equal to stock_price[i]
increment i by 1
if i == n-1
// this condition is true if i value is reached least on end day
break the loop
buy_date[j] = i
increment i by 1
// find local maxima which are ending index.
while (( i should be less than n ) and stock_price[i] should be greater than or equal to price[i-1])
increment i by 1
sell_date[j] = i-1
num_of_ways_buyssell increment by 1
increment j by 1
step 7: if num_of_ways_buyssell is equal to zero then
display then there is no day to buy the stock which gives profit
else:
for i=0 to j-1
display "Buy day "buy_date[i] "Sell day"sell_date[i]
step 8: stop
Yes, It is possible that every combination of days will lose you money, in which case you should not buy the stock at all and have a profit of $0. This condition is achieved in the algorithm when num_of_ways_buyssell.
The overall time complexity is O(n)
Thank you:)
Can someone help me with a function for this. Also, what BIG O notation would the function be? stock prices change from...
Explain how MPI could be used to speed up a sort a million (1,000,000) values. If you have 1000 processors available how fast would your solution be (big O notation). I do not need code for this -- an explanation of an algorithm and how it would work is sufficient.
I would really appreciate it if someone can help me with the whole question. thank you. You may be familiar with Newton's Second Law of Motion, SF = mā. In English, this equation says: The sum, or net, (S) of the forces (F) acting upon an object equals (=) the mass (m) of the object multiplied by the object's acceleration (ā). Even if you are familiar with this famous equation, did you notice the arrows above F and a before?...
can someone help me with this C++ problem write your game() function and 3+ functions that simulate the game of Jeopardy Dice according to the following game mechanics and specifications. Game Mechanics There are two players: the user and the computer, who alternate turns until one of them reaches 80 points or higher. The user is always the first player and starts the game. If any player reaches 80 points or more at the end of their turn, the game...
[Code in C] Help me with this. Not sure what to do. 1 Couting Sort You may have learned some sorting algorithms - such as bubble sort ad quicksort in CS 110 and CS 210. This homework is about counting sort. Let n be the number of elements to be sorted. Bubble sort and quicksort assume tha time, which one is larger and which one is smaller. They make no assumption on the values of the elements t we can...
QUESTION 6 please help MATLAB to and you 5. MATLAB can also solve second order equations symbolically using the Symbolic packages. The help page https://www.mathworks.com/help/symbolic/solve-a-single-differential-equation. html#f1-11214 shows examples of how this works. Code this up for the same equation and see if you get the same answer. If you don't (and you probably won't), try simplifying the answer after you get it to see if it matches then. Note: You'll need to define the symbolic function y(t) here in order...
Hello, If anyone can help me I will grestly appreciate it! I will also give a thumbs up! Autotech Manufacturing is engaged in the production of replacement parts for automobiles. One plant specializes in the production of two parts: Part 127 and Part 234. Part 127 produces the highest volume of activity, and for many years it was the only part produced by the plant. Five years ago, Part 234 was added. Part 234 was more difficult to manufacture and...
please can you solve for all these questions but the right answers because I dont have anymore remaining please a) What is the market cap? b) What are the earnings per share? (TTM after EPS stands for "trailing twelve months", i.e. their reported earnings per share for the last 12 month period) c) What is their PE ratio? Click on the "Chart" menu item. Click on the "18" option to get a chart of stock prices for the last year....
please help! 2. Analyzing occupancy rates Bill, an economics student, says, "This articie makes no economic sense. It quotes someone as saying that the price of apartments will go up if jobs pick up in Santa Clara. But 5% of the apartments are sitting empty. Prices should go down when there is a surplus like this one." Sara, the graduate teaching assistant for Bil's section, tries to explain to him why he might be wrong. (You have to answer her...
Name: Lab Partner(s) Date: Question What would be the density of lab gas at STP (o C and i atm)? (Hints (You do NOT have to answer these questions in italics; they are only here to help you answer the actual question.): There are TONS of different ways to approach this problem. If you were to bring your sample of lab gas to STP, what (from the ideal gas equation) would change? How would you expect this change to affect...
Hi can You help me make a summary about this short article, and also tell me how it affects me economically as US citizen ? As Government Shutdown Persists, Americans Feel the Bite Members of the Secret Service on Monday outside the White House. Virtually every employee with the agency is required to work during the shutdown.CreditDoug Mills/The New York Times Image Members of the Secret Service on Monday outside the White House. Virtually every employee with the agency is...