Can anybody please tell me why I am getting the wrong answer?

My solution: https://www.codechef.com/viewsolution/38594649

Problem: https://www.codechef.com/LRNDSA01/problems/CARVANS

You have to tell the number of cars which were travelling with their maximum speed. For this, you just calculated the difference in speed of consecutive cars and then checked whether it’s positive or not which is not a correct way to do this problem. Your code will fail on the following test case :

x = [2, 6, 3, 1, 2, 5, 7, 1]

Here your array b is b = [2, -4, 3, 2, -1, -3, -2, 6] and this gives 4 as answer.

You have to check for the cars ahead of the next car too because it’s own speed is dependent on the car next to it and so on. This can be done by calculating the highest speed that the car can achieve using the below code.

```
ans = 0
for(ll i=0; i<n-1; i++){
if(x[i+1] > x[i]){
x[i+1] = x[i];
}
else{
ans += 1;
}
}
```

For, x = [2, 6, 3, 1, 2, 5, 7, 1], we will get x = [2, 2, 2, 1, 1, 1, 1, 1] and now we can see that only 3 cars are moving at their maximum speeds i.e car 1, car 4 and car 8.

You can try yourself for x = [3, 2, 8, 6, 4, 9, 10, 2, 5, 7, 4, 8]