Data Structures & AlgorithmsHard

When counting subarrays with sum divisible by m, why does the prefix-remainder method count pairs of equal remainders?

  1. A. Equal prefix sums are always adjacent.
  2. B. The remainder of a prefix sum is always zero.
  3. C. Two prefix sums with the same remainder mod m differ by a multiple of m, so the subarray between them has sum divisible by m.
  4. D. Only the largest prefix remainder matters.

Correct answer: C. Two prefix sums with the same remainder mod m differ by a multiple of m, so the subarray between them has sum divisible by m.

If P_j ≡ P_i (mod m), then the subarray sum P_j − P_i is divisible by m. Counting equal-residue pairs (plus the seeded zero residue) counts every qualifying subarray once.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic