Data Structures & AlgorithmsVery hardCodeforces Div2 C · TCS NQT

How many non-empty contiguous subarrays of [-4, 2, -7, 5, -1, 3, -2, 6] have a sum divisible by 3 (the sum 0 counts as divisible)?

  1. A. 7
  2. B. 10
  3. C. 9
  4. D. 12

Correct answer: B. 10

Count pairs of prefix sums with equal remainder. Mathematically the remainder is taken in [0, k), but the C/Java % operator keeps the sign of the dividend, so negative prefixes land in the wrong bucket unless you normalise them.

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

Exams that ask this topic