Sushant2308's blog

By Sushant2308, history, 5 weeks ago, In English,

Can anyone please suggest why https://codeforces.com/contest/1220/submission/86035838 this is showing different answer on test case 6 while on other gfg's ide it is showing correct answer Here is the link https://ide.geeksforgeeks.org/H6TCl8VK8S

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

»
5 weeks ago, # |
  Vote: I like it +17 Vote: I do not like it

If your output differs on different platforms, it's practically always some Undefined Behaviour (google that if you don't know what it is).

In the case of your code, it's almost certainly accessing index out of bounds of some array/vector — just carefully check all indices you access in that test. I don't have the time to do that, so I can't guarantee that's the issue, but from experience, I'm fairly sure.

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it -11 Vote: I do not like it

    I don't think I am accessing index out of bound because it runs fine on another online ide as well as machine Btw I was stalking u last night and coincidentally u commented on my query

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it +16 Vote: I do not like it

      for(int i=1;i<=s.length();i++){ if(i==1 || i==s.length() || i<less[i-1] || s.at(i-1)==s.at(less[i-1]-1)) cout <<"Mike" << endl; else cout << "Ann" << endl; }

      In this for loop you are using s.at(less[i-1] — 1), I think when less[i-1] = 0, it tries to access s.at(-1), and that is the reason for the weird behavior I think.

    • »
      »
      »
      4 weeks ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      Undefined behaviour means anything can happen. Unlike Java, where accessing an index out of bounds will just throw an exception immediately, in C++ it may work fine or it may not — it's fairly unpredictable in practice.

      Just because it works fine on your machine or on some other IDE, does not in any way prove there's no undefined behaviour. So I'm still fairly sure it's index-out-of-bounds, and SRIKANTH is probably right in his assessment (although I haven't looked much into it, so can't confirm if that's the issue precisely).