Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

Guaguapi's blog

By Guaguapi, history, 10 months ago, ,

Here is the code : Accepted ; Wrong answer ;

• 0

 » 10 months ago, # | ← Rev. 2 →   0 it also give same wrong answer for C++11your 'tot' operation look unbehaviourit should beX[tot + 1] = X[tot] + 1, tot++;
•  » » 10 months ago, # ^ |   0 You mean that the operation ++tot doesn't same in C++17 and C++14 ? sorry for my poor English.
•  » » » 10 months ago, # ^ |   +18 He means "undefined behavior". It's a kind of coding error and you shouldn't do that.
•  » » 10 months ago, # ^ |   0 it also got accepted with C++ 11 ,here:61043518
 » 10 months ago, # |   0 I could not find any operation for this behaviour.Since it failed on 2nd test case so you can put debug statement with condition (n==5) so it won't fail on 1 case and you can know where it start changing behaviour.If you find the mistake please update in post as well, i would like to know where it fail
•  » » 10 months ago, # ^ |   0 See This answer in stack-overflow. In your submission X[++tot] = X[tot-1] + 1; is undefined behavior for c++ versions less than c++17: The left hand side of the = operator can be executed before or after the right hand side. In c++17, it's guaranteed that the right side will be executed before the left side. I think you assumed that in X[tot-1] tot's value was increased by 1 because of X[++tot], but actually tot is increasing later.Thanks again ShafinKhadem !
 » 10 months ago, # |   +18 See This answer in stack-overflow. In your submission X[++tot] = X[tot-1] + 1; is undefined behavior for c++ versions less than c++17: The left hand side of the = operator can be executed before or after the right hand side. In c++17, it's guaranteed that the right side will be executed before the left side. I think you assumed that in X[tot-1] tot's value was increased by 1 because of X[++tot], but actually tot is increasing later.
•  » » 10 months ago, # ^ |   +2 Thank you very much.
•  » » 10 months ago, # ^ |   +5 That's not only the execution order is undefined. The entire expression is undefined. Anything can happen.
•  » » » 10 months ago, # ^ |   +5 thank you very much,Chinese friend