Data Structures & AlgorithmsVery hardAmazon · Google coding round

For the array [4, 5, 0, -2, -3, 1, 7, -9, 6] and k = 5, how many non-empty contiguous subarrays have a sum divisible by k?

  1. A. 11
  2. B. 14
  3. C. 9
  4. D. 12

Correct answer: D. 12

Count prefix sums with equal remainder mod 5 (normalised to be non-negative) and seed the count of remainder 0 with 1 for the empty prefix. This gives 12; omitting the seed gives 11.

More Coding Interview Problems questions

Exams that ask this topic