I've written my solution for this task https://mirror.codeforces.com/contest/702/problem/A When I submitted it I got the wrong answer on test 1 https://mirror.codeforces.com/contest/702/submission/287285925 My code outputs 4, instead of 3 which is the correct answer.
I reran the exact same code using the same input as in test 1 but got 3. I even tried to run it on a few online cpp compilers but they all still outputted 3. So my question is, how come only on Codeforces does my code output 4 instead of 3? Could it be that my code uses something that is not supported on Codeforces or have I simply made a mistake in my code?
Inconsistent outputs for the same inputs (in the absence of using random functions) typically means something in the code is causing undefined behaviour — looking at your code a likely candidate is accessing an element out of bounds. Are you sure that on
a[x] >= a[x + 1]
the value ofx+1
is always in bounds?You're right. This
if (x < a.size() - 1)
was wrong. When x is same as a.size()-2 that is already the last sliding window. But I incremented it one more time so thex+1
ended out of bounds. Now I've changed it toif (x < a.size() - 2)
and it got accepted. Thanks for pointing me in the right direction.