2nd testcase is not passing

Hello @akh.chakraborty11,

Run your code for:
5
39
45
40
42
43

Expected Output:
1 2 1 2 3 END

Your Output:
1 2 1 4 5 END

Correct your code and it will satisfy all the testcases.

Hint:
You are just checking for the element before the current element and if current element is greater, then you are printing it’s position in the array as span for that stock price. This approach will fail for above mentioned testcase.

Hope, this would help.
Give a like, if you are satisfied.

it says

A span of a stock’s price on a given day, i, is the maximum number of consecutive days before the (i+1)th day, for which stock’s price on these days is less than or equal to that on the ith day.

and says 1<=i<=n
for i=5 how will it work? I am confused

If you are asking about the last element,
In our example, the element at i=5 is 43:
There are two span possible:

  1. 1 i.e. for 39
  2. 3 i.e. for 40 42 43
    [as we have to consider only consecutive/continuous positions for which element is either less than or equal the current element i.e. 43 in this case]

As, we have to select the maximum,
Span for i=5, is 3.

Hope, I have cleared your doubt.

1 Like