Slow Trains (BUG4EVER 2017) Weird Issue

Here are two of my submissions, exactly same, one in C and the other in C++. The submission in C++ gives an AC verdict while the submission in C gives a WA verdict. The code was originally written in C. Can anyone please point out the error? I’m unable to find the problem because the two submissions are EXACTLY SAME. (only iostream line was different)

[Solution 1][1]

[Solution 2][2]



That’s weird.

I am also having the same problem…would be grateful if someone could figure it out…
solution link:

Quite weird actually. Are you sure that TCs werent changed mid way?

nope the TC’s were same
here’s the link to the problem:

yeah, TC’s were same @vijju123 as most of the people’s solution are 100% similar to mine. It’s just if and else lol. Wasted 2 hours figuring out error in my logic lol

What was your error? What difference was in C and C++ code?

the competition wasn’t fair though

no error no difference

Well, got the error guys. You’re declaring variables inside the loop which sometimes don’t work well in C Language. Look at the solution if declared outside your code works fine in C. Solution

1 Like

If you dont know the cause, how did you conclude :stuck_out_tongue:

Simply, iterating through all the possibilities where an error should occur and that was the line which was causing the error when declared outside, works like a charm.

Well, now I got the cause. That’s a standard difference. In C99, the rules are the same as in C++. Default GCC uses C89 with some extensions. For C89, you must declare all of your variables at the beginning of a scope block.

Thanks, never knew that it makes a difference nowadays.
I had read that nowadays it doesn’t matter :
Maybe I was wrong lol.
I’ll declare everything global now :stuck_out_tongue:


Most of the compiler use C99 but still some Online judges use C89 as gcc. Declaring global will also help you to use more space for allocation. Better to use C++, it also has some inbuilt implementations to make the job easy.