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.

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.

[ad type=”banner”]

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.

C++ Programming:

[pastacode lang=”cpp” manual=”%2F%2F%20a%20linear%20time%20solution%20for%20stock%20span%20problem%0A%23include%20%3Ciostream%3E%0A%23include%20%3Cstack%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20stack%20based%20efficient%20method%20to%20calculate%20stock%20span%20values%0Avoid%20calculateSpan(int%20price%5B%5D%2C%20int%20n%2C%20int%20S%5B%5D)%0A%7B%0A%20%20%20%2F%2F%20Create%20a%20stack%20and%20push%20index%20of%20first%20element%20to%20it%0A%20%20%20stack%3Cint%3E%20st%3B%0A%20%20%20st.push(0)%3B%0A%20%0A%20%20%20%2F%2F%20Span%20value%20of%20first%20element%20is%20always%201%0A%20%20%20S%5B0%5D%20%3D%201%3B%0A%20%0A%20%20%20%2F%2F%20Calculate%20span%20values%20for%20rest%20of%20the%20elements%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%20%2F%2F%20Pop%20elements%20from%20stack%20while%20stack%20is%20not%20empty%20and%20top%20of%0A%20%20%20%20%20%20%2F%2F%20stack%20is%20smaller%20than%20price%5Bi%5D%0A%20%20%20%20%20%20while%20(!st.empty()%20%26%26%20price%5Bst.top()%5D%20%3C%3D%20price%5Bi%5D)%0A%20%20%20%20%20%20%20%20%20st.pop()%3B%0A%20%0A%20%20%20%20%20%20%2F%2F%20If%20stack%20becomes%20empty%2C%20then%20price%5Bi%5D%20is%20greater%20than%20all%20elements%0A%20%20%20%20%20%20%2F%2F%20on%20left%20of%20it%2C%20i.e.%2C%20price%5B0%5D%2C%20price%5B1%5D%2C..price%5Bi-1%5D.%20%20Else%20price%5Bi%5D%0A%20%20%20%20%20%20%2F%2F%20is%20greater%20than%20elements%20after%20top%20of%20stack%0A%20%20%20%20%20%20S%5Bi%5D%20%3D%20(st.empty())%3F%20(i%20%2B%201)%20%3A%20(i%20-%20st.top())%3B%0A%20%0A%20%20%20%20%20%20%2F%2F%20Push%20this%20element%20to%20stack%0A%20%20%20%20%20%20st.push(i)%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%20cout%20%3C%3C%20arr%5Bi%5D%20%3C%3C%20%22%20%22%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”/]

Output:

1 1 2 4 5 1

Time Complexity: O(n). It seems more than O(n) at first look. If we take a closer look, we can observe that every element of array is added and removed from stack at most once. So there are total 2n operations at most. Assuming that a stack operation takes O(1) time, we can say that the time complexity is O(n).

Auxiliary Space: O(n) in worst case when all elements are sorted in decreasing order.

[ad type=”banner”]