Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many non-empty contiguous subarrays of [4, 5, 0, -2, -3, 1] have a sum divisible by 5 (including negative sums, which are divisible when the remainder is 0)?

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

Correct answer: B. 7

Equal prefix sums modulo 5 mark divisible subarrays. Counting pairs of equal remainders, with the initial prefix 0 included, gives the count.

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

Exams that ask this topic