Here are my 2 submissions for the problem — https://codeforces.net/contest/1541/problem/B
Only difference between the two is the if condition — in first if(l<r and var==(l+r) and in second if(l<r and var%(l+r)==0 and (var/(l+r))==1)
Ideally both should produce same result but on test 103 it is failing - The test 103 is this
n=4 a=5 2 4 1
its answer should be 1 and indeed its 1 when i run in local or custom invoction but on submit it is giving answer 2 hence WA
Can someone pls help why is behaviour?
Learning to debug is something you should master. :) Try searching for debug templates or learn how to stress test.
I did that and the output is also fine, only getting WA on submit
You have undefined behavior in your code. The size of
mp
ismx+1
,i
andj
might have valuesmx+1
and you're callingmp[i]
andmp[j]
. With UB your code might sometimes work and sometimes fail — you got lucky with your other solution. This really doesn't have anything to do with the line of code you changed.Thanks, it was a silly mistake, but still it got accepted only when i changed that line of code, and regarding getting lucky, i submitted multiple times still was getting WA on the first and AC on the changed code
Hmm... It might not've been luck in the sense that "every time I submit there is a 10% chance the code passes" but instead, maybe it was that "this small change accidentally made the UB not matter". I honestly don't know. My point is that with UB, you can't be sure if your code will fail or not.