Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

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

  1. A. 8
  2. B. 31
  3. C. 3
  4. D. 6

Correct answer: D. 6

Store the first index of each prefix residue mod 3; the longest gap gives [4, 1, 5, 9, 2, 6] with sum 27, length 6. The whole array (8) sums to 31, which is not divisible by 3; 31 is that sum, and 3 is a shorter valid window.

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

Exams that ask this topic