1 testcase fail

1 testcase give wrong output
code link:https://ide.codingblocks.com/s/142780

Hey @Rohan_singla,
In the code snippet below:
for(int i=prices.length-1;i>=0;i–){
j=i;
int count=1;
while(j>0 && prices[j]>=prices[j-1]){
count++;
j–;
}
stack.push(count);
}

you are comparing prices[j] with prices[j-1] and then decrementing j. Instead do prices[i]>=prices[j-1] and decrement j as you are doing.

hey @sanchit.bansal06
why i have to do thisn change

Hi @Rohan_singla,
We are doing this because if you do a dry run:
say if array length = 5.
We will start with i = 4, then:
j=i -> j = 4
while loop: prices[4]>=prices[3]
in the next iteration prices[3]>=prices[2]
and so on.
Now, it might be possible that, prices[4] is greater than prices[3] but prices[2] is also greater than prices[3] but smaller than prices[4]. In this case you will get a wrong answer. That’s we will always compare only the current element with the previous elements, that is prices[i] with prices[j-1]

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.