Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional
For a = [1, 2, 3, 4, 5, 6, 7, 8], how many contiguous subarrays have a sum that is a multiple of 5?
- A. 7
- B. 14
- C. 8
- D. 10
Correct answer: D. 10
Count pairs of prefix sums with the same residue mod 5, including the empty prefix. The residue-1 test is a common confusion: it counts a different class of subarrays and gives another number.
More Coding Interview Problems questions
- Array a = [2, 1, 5, 2, 3, 1, 4] has only positive values. How many non-empty contiguous subarrays have sum at most 9?
- Sorted values [1, 4, 6, 9, 13, 14, 18, 22] must be split into the fewest contiguous groups so that in every group the largest value minus…
- Two multisets A = {7, 2, 9, 4, 5} and B = {3, 8, 1, 6, 10} are matched one-to-one, pairing each element of A with exactly one element of…
- How many permutations of (1, 2, 3, 4, 5) have every pair of adjacent entries differing by at least 2? Solve with a bitmask DP that…
- How many integers x with 0 <= x <= 1000 have exactly four 1-bits in their binary representation?
- Among the values [3, 10, 5, 25, 2, 8], what is the maximum value of x XOR y over two different elements x and y?