Consider the testcase:
0 0 0 1 0
Answer should be 2, your code outputs 3.
I just had a look at the editorial to this. Turns out my algorithm was wrong.
I am surprised by the number of test cases it passed even though the algorithm was wrong.