Data Structures & AlgorithmsHardGATE CS · competitive programming

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

  1. A. 6
  2. B. 7
  3. C. 2
  4. D. 21

Correct answer: B. 7

Pairs of equal prefix sums modulo 5 mark the subarrays: [4,5,0,2,3,1], [5], [5,0], [5,0,2,3], [0], [0,2,3] and [2,3], so 7. Forgetting the initial prefix 0 drops the whole array (6); 2 counts only single elements and 21 counts every subarray.

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

Exams that ask this topic