Why one solution got AC while other WA


i am getting AC if i use cin to get “n”…but getting wrong answer when using scanf.
can anyone please help?

Why is this happening?
@algmyr @carre @galencolin
I initially thought it’s due to undefined behavior, but there doesn’t seem to be any such behavior in the code.

Found a similar issue here.


It gives AC in C++ 17.


Weird. :no_mouth:


This language is so confusing. :man_shrugging: :man_shrugging:

1 Like

So true! Does using such incoherent macros really speed up your typing? I don’t think so. Never a fan of loop macros and rep macros :stuck_out_tongue:


I agree!

1 Like

I don’t have an answer but as suggested by akshmit and aneee I would remove that huge amount of macros


Yeah this is so weird , i mean if this was the reason for WA then what will the participant do?

1 Like



So it was undefined behaviour after all. That’s makes sense.

1 Like

I want to stand up for macros - I use a terrible IDE, so I need to write code fast to balance out the issues it causes :stuck_out_tongue:

(but definitely, when asking for help or showcasing code, try to get rid of them)


Just curious…which ide do you use by the way?

Yet another case where just compiling with sanitizers immediately gives away the issue. Compiling with -fsanitize=address,undefined and running against the sample shows it’s an out of bounds access. So it’s UB.

Specifically the a[ans] is out of bounds, indexed by ans = n+1. Your code randomly passed because it’s UB and you depend on what is just after your allocated memory (which can be anything).

(Unrelated, but please consider not using variable length arrays (array with a variable size). They are not standard C++, use a vector like sane people)


Isn’t accessing out of bounds of a vector also UB? I remember resizing a vector to n, and accessing v[n]. It gave some junk value.

Like I said, it’s unrelated. But usage of VLAs just bugs me. :wink:

Though with vector you can compile with flags like -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC which will notify you of an out of bounds access. With vector you have more tools at your disposal (and it’s standard C++).

For a reference of useful things to know, see CF post “Catching silly mistakes with GCC”.


This is nothing new :joy:

That kind of thing is almost exclusively due to code with undefined behavior.

1 Like

Please point out the specific lines. That wold be helpful.

I don’t know. I haven’t thoroughly looked at your code. I saw nothing obvious at a cursory glance, doesn’t mean there is nothing there though.

1 Like