The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate span of stock’s price for all n days.
The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.
For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}

A Simple but inefficient method
Traverse the input price array. For every element being visited, traverse elements on left of it and increment the span value of it while elements on the left side are smaller.

Following is implementation of this method.

C Programming:

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20for%20brute%20force%20method%20to%20calculate%20stock%20span%20values%0A%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20Fills%20array%20S%5B%5D%20with%20span%20values%0Avoid%20calculateSpan(int%20price%5B%5D%2C%20int%20n%2C%20int%20S%5B%5D)%0A%7B%0A%20%20%20%2F%2F%20Span%20value%20of%20first%20day%20is%20always%201%0A%20%20%20S%5B0%5D%20%3D%201%3B%0A%20%0A%20%20%20%2F%2F%20Calculate%20span%20value%20of%20remaining%20days%20by%20linearly%20checking%0A%20%20%20%2F%2F%20previous%20days%0A%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%7B%0A%20%20%20%20%20%20S%5Bi%5D%20%3D%201%3B%20%2F%2F%20Initialize%20span%20value%0A%20%0A%20%20%20%20%20%20%2F%2F%20Traverse%20left%20while%20the%20next%20element%20on%20left%20is%20smaller%0A%20%20%20%20%20%20%2F%2F%20than%20price%5Bi%5D%0A%20%20%20%20%20%20for%20(int%20j%20%3D%20i-1%3B%20(j%3E%3D0)%26%26(price%5Bi%5D%3E%3Dprice%5Bj%5D)%3B%20j–)%0A%20%20%20%20%20%20%20%20%20%20S%5Bi%5D%2B%2B%3B%0A%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20elements%20of%20array%0Avoid%20printArray(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20printf(%22%25d%20%22%2C%20arr%5Bi%5D)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20price%5B%5D%20%3D%20%7B10%2C%204%2C%205%2C%2090%2C%20120%2C%2080%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(price)%2Fsizeof(price%5B0%5D)%3B%0A%20%20%20%20int%20S%5Bn%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Fill%20the%20span%20values%20in%20array%20S%5B%5D%0A%20%20%20%20calculateSpan(price%2C%20n%2C%20S)%3B%0A%20%0A%20%20%20%20%2F%2F%20print%20the%20calculated%20span%20values%0A%20%20%20%20printArray(S%2C%20n)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

1 1 2 4 5 1

Time Complexity of the above method is O(n^2). We can calculate stock span values in O(n) time.

A Linear Time Complexity Method
We see that S[i] on day i can be easily computed if we know the closest day preceding i, such that the price is greater than on that day than the price on day i. If such a day exists, let’s call it h(i), otherwise, we define h(i) = -1.
The span is now computed as S[i] = i – h(i). See the following diagram.

To implement this logic, we use a stack as an abstract data type to store the days i, h(i), h(h(i)) and so on. When we go from day i-1 to i, we pop the days when the price of the stock was less than or equal to price[i] and then push the value of day i back into the stack.

[ad type=”banner”]