118

My C++ code is working fine giving the correct output in my local Code Editor bu...

 2 years ago
source link: http://codeforces.com/blog/entry/98163
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Hello, I am new and just starting with competitive programming so I am not quite sure as to why this problem has occurred

My Question Problem was-(https://codeforces.com/contest/1612/problem/B)

And My Solution to it was (http://codeforces.com/contest/1612/submission/140203265)

Is there something I am overlooking or is something wrong with this code?

7 hours ago, # |

I submitted the code as yours without changing anything, and it got accepted.

C++17 same code without changes

But the difference was in the language. You used C++20 while I submitted it in C++17. Generally, C++20 is more sensitive to indexing and other minute stuff, which I guess is ignored in C++17 (Personal Opinion IDK if it's true).

So coming towards your error in the code was in the while loop --> while(c<=(n-2)) in this loop, the value of i(index of the array) was going beyond the size, and thus, it was giving run time error because it was going out of bounds. For correction, you can use

while(c<=(n-2) && i<n) --> this will ensure that your indexing stays in the limit.

I submitted your code in C++20 after adding changing that while condition looks like

C++20 Code after changes (ignore the condition for j, it works without that, too, though j should be greater than 0 in all cases).

Happy Coding :)

  • 5 hours ago, # ^ |

    Yes Thank You The i<n seemed to be the error that I overlooked. Thank you for that. But you said the j condition can be overlooked ..wouldn't that lead to the insertion of duplicates in the array? like for input — 6 2 5 without the j condition, my array would look something like 2 6 5 5 4 3 ?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK