Блог пользователя PR3S1D3NT

Автор PR3S1D3NT, история, 3 года назад, По-английски

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://mirror.codeforces.com/contest/1612/problem/B)

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

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

  • Проголосовать: нравится
  • -4
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 :)

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 ?

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      No, the j condition can be overlooked only in the while loop, like your while loop should have the condition of c and i. It will be better if you check j also. Else your code is correct