Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA

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

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

Correct answer: C. 7

Store the first index of each prefix sum mod 4 with prefix 0 at index −1. The total 12 is divisible by 4, so the whole array (7) works. Without that seed the best gap is 5; 12 is the sum and 3 a shorter valid window such as [1, 1, 2].

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

Exams that ask this topic