Data Structures & AlgorithmsVery hardGoogle · Arrays

How many contiguous subarrays of [4, 5, 0, -2, -3, 1] have a sum divisible by 5?

  1. A. 5
  2. B. 6
  3. C. 8
  4. D. 7

Correct answer: D. 7

Prefix sums mod 5 are 0, 4, 4, 4, 2, 4, 0 (counting the empty prefix); pairs of equal residues give 1 + 6 = 7 subarrays.

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

Exams that ask this topic