There is a mistake in the code!

the first if condition should be if(pre==0) ans not if(arr[i]==0)

Yes, you are right ,that ll be pre==0