Data Structures & AlgorithmsHardGATE CS · competitive programming
How many non-empty contiguous subarrays of [4, 5, 0, 2, 3, 1] have a sum divisible by 5?
- A. 6
- B. 7
- C. 2
- D. 21
Correct answer: B. 7
Pairs of equal prefix sums modulo 5 mark the subarrays: [4,5,0,2,3,1], [5], [5,0], [5,0,2,3], [0], [0,2,3] and [2,3], so 7. Forgetting the initial prefix 0 drops the whole array (6); 2 counts only single elements and 21 counts every subarray.
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
- Finding the k-th smallest element with a median-of-medians pivot is used as a deterministic selection method. What is its worst-case time…
- Four sorted lists of sizes 7, 5, 3 and 2 are merged pairwise, always merging the two currently smallest lists first. What is the total…
- How many non-empty contiguous subarrays of [3, 5, 6, 1, 2, 3, 4] have bitwise XOR equal to 0?
- How many non-empty contiguous subarrays of [2, 2, 3, 3, 3, 2] consist of one repeated value only?
- How many non-empty contiguous subarrays of [1, 2, 4, 3, 6, 5, 7, 8] contain equally many even and odd numbers?
- What is the length of the longest contiguous subarray of [5, 3, 4, 2, 1, 9, 6] whose sum is divisible by 6?