Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional

For a = [1, 2, 3, 4, 5, 6, 7, 8], how many contiguous subarrays have a sum that is a multiple of 5?

  1. A. 7
  2. B. 14
  3. C. 8
  4. D. 10

Correct answer: D. 10

Count pairs of prefix sums with the same residue mod 5, including the empty prefix. The residue-1 test is a common confusion: it counts a different class of subarrays and gives another number.

More Coding Interview Problems questions

Exams that ask this topic