Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

What is the length of the longest contiguous subarray of [5, 3, -2, 8, -4, 6, 1, 7] whose sum is divisible by 5 (the empty sum 0 counts as divisible)?

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

Correct answer: B. 5

Record the FIRST index where each prefix-sum remainder occurs; a later index with the same remainder closes a divisible subarray, and the longest such gap is the answer. Recording the last index instead gives shorter subarrays.

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

Exams that ask this topic