Stock Span. 3/4 test cases passed , one says wrong answer

@lousybrick
you are getting wrong output for this test case
Input:
5
30
40
35
45
42

Output:
1 2 1 4 1 END

I’ve just tested out using the same values and got an output of 1 2 1 2 1 END. My main doubt over here is what would the span value be if two consecutive prices are the same, like if its 30 35 35 45 should my output be like 1 2 2 3 or 1 2 1 2 ?

@lousybrick
the output for 30,35,35,45
should be this 1 2 1 4

I have modified the code but I’m still getting a wrong answer for that particular test case.


Could you please point out where the mistake is at ?

@lousybrick here is the correct code.


few mistakes in your code

  1. you have to add the index of the elements so that you know the index of the just greater element.
    2.you have to use while loop until prices[stack.top]<prices[i]

if you simply add element in the stack then you wouldn’t know how far the greater element is from the present element.

1 Like

@lousybrick
please mark your doubt as resolved in my doubt section and rate me as well. :blush: :blush:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like