Stock Span

The span si of a stock’s price on a certain day i is the minimum number of consecutive days (up to the current day) the price of the stock has been less than or equal to its price on that ith day. If for a particular day, if no stock price is greater then just count the number of days till 0th day including current day.

For eg. if given price array is {3, 6, 8, 1, 2}, span for 4th day (which has price 2) is 2 because minimum count of consecutive days (including 4th day itself) from current day which has price less than 4th day is 2 (i.e. day 3 & 4). Similarly, span for 2nd day is 3 because no stock price in left is greater than 2nd day's price. So count is 3 till 0th day.

Given an input array with all stock prices, you need to return the array with corresponding spans of each day.

Note : Try doing it in O(n).

Input format :

Line 1 : n - Arrays Size

Line 2 : Price for n days (separated by space). It can contain duplicate values

Output format : Return an array that contain span for each day

Sample Input 1 :
60 70 80 100 90 75 80 120
Sample Output 1 :
1 2 3 4 1 1 2 8

Login to submit your solution

Fb dab53a1a64929320dc7425896aa73fd9e5512b6ab52e414ff5f3ebb5cecc73eb

Share Problem