PR3S1D3NT's blog

By PR3S1D3NT, history, 3 years ago, In English

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.net/contest/1612/problem/B)

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

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

  • Vote: I like it
  • -4
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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