Data Structures & AlgorithmsHard

What is the length of the longest contiguous subarray of 1 2 3 5 1 2 3 such that the sum of EVERY prefix of that subarray is divisible by 3?

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

Correct answer: C. 1

Every prefix of the block must have residue zero mod 3, so the first element must already be divisible by 3 and each later step must keep the running sum at residue zero. Requiring only the total sum to be divisible by 3 is a weaker condition.

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

Exams that ask this topic