shreypandey's blog

By shreypandey, history, 7 years ago, In English

Hi guys, I am just starting to code in C++. I keep hitting a runtime error for my solution 28288661. It works fine on Ideone, for the 2nd testcase, which is where it fails on Codeforces. Can someone help me?

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

»
7 years ago, # |
  Vote: I like it +4 Vote: I do not like it

arr[i].size() is an unsigned value (size_t), and arr[i].size()-1 may overflow if arr[i] is empty, which is what happens on the second case. You can check that if you add assert(!arr[i].empty()). After such overflow your program will yield array out-of-bounds-based undefined behavior.