Data Structures & AlgorithmsHard

How many contiguous subarrays of [1, 5, 2, 8, 3, 7] have a sum divisible by their own length?

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

Correct answer: D. 14

Test each subarray's own length: the average is an integer exactly when the sum is divisible by the subarray's length. Using the full array length or the complement gives a different count.

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

Exams that ask this topic