Why one solution got AC while other WA

I shouldn’t say, it’s technically public, but only if you’re actively looking for it (it’s not like ideone with a “recent” page)

If the issue is resolved can anyone tell what was the reason for WA?

See the final print statement in both the submissions. ans is initialised as n + 1, and finally array[ans] is accessed. So it’s access out of bounds, and that gives some junk value. Which is undefined behaviour, and the solution passed by chance. Nothing to do with input method.

2 Likes

This is so weird if it passed by chance !

1 Like

Haha, maybe. But it’s just a small edge case that’s missed here. And it’s not that uncommon. It happens quite often.

1 Like

Yes, it happened with me too!

1 Like

so even you don’t use a personal ide …why’s that…?just curious again :slight_smile:

Because I am stubborn (and stupid)

don’t be like me lmao

Got it … :zipper_mouth_face:

It’s not weird. Your problem has UB and you’re reading some random memory. The different code ends up with different stuff allocated on the heap, in one of the cases you happened to read data that happened to work out for you.

If you have UB in your code then you can have whatever happen. I don’t think I’ve seen this kind of case (changing something unrelated changes the output) where the answer wasn’t “your code has UB, you were lucky once, learn to use sanitizers”.

1 Like

what you do when you get frustrated while solving a problem?

i got it… thank you everyone for the clarification…and special thanks to @algmyr.:+1:

Umm, I’d go do a different problem, and come back with a fresh mind. If I still can’t solve it I’d look up the editorial if it’s available. If it’s an ongoing contest I would just skip it.

Can you tell what is UB?

It means undefined behavior. You can’t predict how the code is going to flow.
Example:

int A[3] = {};
if(A[3] == 0)
    cout << "1";
else
    cout << "0";

We do not know what will be printed here. We might get SIGSEGV, or something random.

1 Like

Thank you so much @aneee004 i got it!!

1 Like