Data Structures & AlgorithmsHard

How many contiguous subarrays of 3 5 2 6 1 7 4 8 with length between 2 and 4 inclusive have a sum divisible by 4?

  1. A. 8
  2. B. 15
  3. C. 5
  4. D. 12

Correct answer: A. 8

Restrict first to blocks of length 2 to 4, then test the sum modulo 4; both conditions apply to each block. Dropping the length bound, shrinking it, or testing divisibility by 2 gives other totals.

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

Exams that ask this topic