rahulnagurtha's blog

By rahulnagurtha, history, 8 years ago, In English

While I was solving this question during virtual contest, this submission gave me Memory Limit Exceeded verdict, when I changed the code slightly (I just made "vector" ans a global vector) it was accepted, here is the accepted version. The point to observe here is that "vector ans" is of size 193 only, which is very small to cross the given memory limits. I couldn't figure out what is wrong with my code. Any help is appreciated.

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

»
8 years ago, # |
Rev. 2   Vote: I like it +27 Vote: I do not like it

The last element of your "primes" will be 997 and beyond this, it contains undefined values (technically, it is an undefined behaviour and one cannot even assume that it contains something). If the undefined value at the end of "primes" have some property (e.g. zero, one, or some negative values (overflow can complicates the thing)), "tmp" never exceeds "n" and it will loop forever. Thus it will push_back repeatedly and will eventually be MLE.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    Oh yeah ! I found my mistake, Thanks for the help :)

    • »
      »
      »
      8 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I just changed the for loop structure to

      for (int i = 0; i < primes.size() && primes[i] <= n; ++i) {}

      And it worked fine !!