ProgrammingVery hard

A modular sum over 10^6 terms uses sum = (sum + a[i]) % MOD with a[i] < MOD and sum an int. What is wrong?

  1. A. sum + a[i] can reach about 2 x 10^9 and overflow int, so sum must be a long long or the addition reduced first
  2. B. The modulo operation is undefined for int
  3. C. Nothing, because int addition wraps safely
  4. D. The loop is too slow for 10^6 terms

Correct answer: A. sum + a[i] can reach about 2 x 10^9 and overflow int, so sum must be a long long or the addition reduced first

With MOD near 10^9, the sum of two values can exceed INT_MAX, which is undefined. A 64-bit sum avoids this.

More C Programming questions

Exams that ask this topic