Data Structures & AlgorithmsHardCAT · ICPC regional

What is the length of the longest contiguous subarray of [5, 3, 4, 2, 1, 9, 6] whose sum is divisible by 6?

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

Correct answer: D. 7

Store the first index of each prefix residue mod 6 with residue 0 seeded at index −1. The total 30 is divisible by 6, so the whole array (length 7) qualifies. Without the seed the best span found is 4; 6 and 3 are lengths of shorter valid windows, not the longest.

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

Exams that ask this topic