Question: can you explain an algorithm used to solve this problem without coding it: You are given an array a1...an of stock prices of a commodity
can you explain an algorithm used to solve this problem without coding it:

You are given an array a1...an of stock prices of a commodity on each day, i.e., ai is the price of the stock on day i. The span of a stock price on day i is the number of consecutive days before day i for which the price of the stock is less than or equal to ai. You may assume that the price of the stock on day 0 is o, so that the span of the stock on day 1 is 0. For example, if the prices were [7,2,4,6,8,5,6], then the span of the stock on day 4 is 2 as there are two consecutive days (days 2 and 3) in which the prices were less than or equal to the price of the stock on day 4 (which is 6). As other examples, the span of the stock on day 5 is 4, and day 7 is 1. Please design and analyze an algorithm that computes the span of the stock on each day. So the output is an array (say b), where bi is the span of the stock on day i. For full credit, your algorithm should run as fast as possible
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
