protons's blog

By protons, history, 6 weeks ago, In English

In Problem D for the first test case (last case)
6 1 6 3 4 5 6 My answer was 10 everywhere (I tried on VsCode, GFG ide, Codechef ide) but when I was submitting it on Codeforces it was showing 16 and it wasted all of my time. Can Anyone please tell me what could be the probable reason?

 
 
 
 
  • Vote: I like it
  • -4
  • Vote: I do not like it

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by protons (previous revision, new revision, compare).

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

It'll be easier if you add submission link as well

»
6 weeks ago, # |
  Vote: I like it +6 Vote: I do not like it
for (ll i = n; i >= 0; i--)

Just change this to

for (ll i = n; i >= 1; i--)

Since your array is 1 index based it just carrying value to 0 index and also during addition loop it is getting added to your answer , i resubmitted the corrected code but it failed on Case 2

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Thank You callmepandey for pointing it out. But then how on all local ide (Vs code,gfg ide, etc...) it was showing 10.

    All the time I was resubmitting it and getting WA and got ruined my contest.

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Yes that's what i am thinking , for these kind of cases i prefer codechef ide, Actually yesterday even my G was giving correct on my Machine but WA in test 1 in Codeforces later i found that i actually haven't initialised values to one of my array , I guess local ide consider non initialised arrays value as 0 where this is not the case of Codeforces and other Judges.

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it +10 Vote: I do not like it

      When local variables are not initialised, a garbage value is stored. Since you are declaring your array locally, some garbage value is present at index 0. Since this is garbage, it differs in different platforms.

»
6 weeks ago, # |
  Vote: I like it +18 Vote: I do not like it

For the hundreds of such doubts that have preceded and will follow: the compiler is not buggy. It's always undefined behaviour.