Why one solution got AC while other WA

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)

2 Likes

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)

5 Likes

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”.

4 Likes

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

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?