One test case failing


one test case failing

hello @payal_05 this is not the correct way of doing this question .
this questions code is typically implemented with the help of stack.
6
10 4 5 90 120 80
try for this test case .
your output:1 1 2 3 4 1 END
expected output : 1 1 2 4 5 1

there are so many test cases like this .

I didn’t understand the implementation. Can you explain it once?

@payal_05 yes ,sure
in this question you have to maintain a stack in which you will take input your array indices ,
and your implementation would be some like if you are on any particular index then you will compare that element from its previous elements if they are greater like you will start popping out elements until you find that your present element is smaller than its previous elements .
then you will subtract the index on top of the stack from ith position this will give you answer for that particular index .
suppose like if we take the example of the test case i mentioned above .
INPUT:
6
10 4 5 90 120 80
in this initially your stack is empty so first comes 1 .
then you push the index of element 4 i.e 1 then you will check whether the present element is greater than its previous elements and if yes then you have upto which index this condition is true .
as 4 is not greater than 10 that’s why we will not pop from stack .and update the our answer as i-s.top() which is 1 in this case .
then the next element is 5 and we will do the same step we will check for its previous elements and as 5 is greater than 4 so we will pop 4 and and then we will check for the element which is on the top of the stack as 10 is not smaller than 5 we will terminate our while loop and update our cuurent answer as i-s.top() i.e 2 in this case.;

here for your reference i am giving you the code link for the same implementation.

hey @payal_05 if you feel that your doubt is cleared you can now mark this doubt as cleared .

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.