Data Structures & AlgorithmsHard

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

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

Correct answer: C. 6

Store the FIRST index at which each prefix remainder appears; the longest equal-remainder gap is then i − first[r]. Overwriting with the latest index shortens the answer, and forgetting the prefix 0 loses subarrays that begin at index 0.

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

Exams that ask this topic