Data Structures & AlgorithmsHardCAT · ICPC regional
What is the length of the longest contiguous subarray of [5, 3, 4, 2, 1, 9, 6] whose sum is divisible by 6?
- A. 6
- B. 4
- C. 3
- D. 7
Correct answer: D. 7
Store the first index of each prefix residue mod 6 with residue 0 seeded at index −1. The total 30 is divisible by 6, so the whole array (length 7) qualifies. Without the seed the best span found is 4; 6 and 3 are lengths of shorter valid windows, not the longest.
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 index pairs i < j satisfy a[i] - i = a[j] - j for the array [7, 3, 5, 8, 6, 9, 4, 10] (0-based indices)?
- How many inversions (pairs i < j with a[i] > a[j]) does the array [3, 1, 4, 1, 5, 9, 2, 6] contain?
- How many index triples i < j < k in the array [1, 2, 3, 4, 5, 6] have a[i] + a[j] + a[k] divisible by 5?
- A queue has capacity 4 and an enqueue into a full queue is rejected. Operations in order: enqueue 1, 2, 3, 4; dequeue; dequeue; enqueue 5…
- What is the sum of the maxima of every contiguous window of size 3 in the array [1, 3, -1, -3, 5, 3, 6, 7]?
- How many valid sequences of 4 push and 4 pop operations are there on an initially empty stack, where every pop is applied only to a…