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?
- A. 4
- B. 1
- C. 6
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many contiguous subarrays of [1, 2, 1, 3, 4, 2, 3] contain no repeated value?
- Count the inversions of [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]: pairs of positions i < j with N[i] > N[j]. Equal values are not inversions.
- How many indices i satisfy: the sum of the elements before i equals the sum of the elements after i, excluding P2[i] itself? (P2 = [1, 2…
- How many DISTINCT values appear among the sums of all non-empty contiguous subarrays of [1, 2, 3, 1, 2, 3]?
- How many contiguous subarrays of [6, 10, 15, 4, 9] have gcd equal to 1?
- How many contiguous subarrays of [1, 3, 5, 7, 2, 4] have a sum that is a perfect square (0 counts as a square)?