Data Structures & AlgorithmsHard

What is the length of the shortest contiguous subarray of 3 8 2 5 1 4 6 whose sum is divisible by 5?

  1. A. 2
  2. B. 5
  3. C. 0
  4. D. 1

Correct answer: D. 1

Check subarray sums modulo 5 in order of increasing length; a residue repeat among prefix sums gives the same test in linear time. Excluding singletons, overshooting by one, or taking the longest qualifying block answers different questions.

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

Exams that ask this topic