Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional
Array a = [3, 1, 4, 1, 5, 9, 2, 6]. How many subarrays of length exactly 3 have an even sum?
- A. 5
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
Prefix parities turn the window test into a parity comparison: a length-3 window has even sum when the prefix parities at its two ends agree. Sliding the window and counting parity flips gives the same result as checking each window directly.
More Coding Interview Problems questions
- Array a = [2, 4, 6, 1, 3, 5, 8, 7]. How many contiguous subarrays of length at least 2 have a sum divisible by 4?
- From the sorted side lengths [4, 6, 7, 9, 10, 13, 15], how many different triples (choosing distinct positions) can form a non-degenerate…
- Sorted arrays A = [1, 7, 12, 20, 31] and B = [3, 9, 15, 25, 40]. What is the smallest value of |x - y| over x in A and y in B?
- Values [3, 9, 4, 15, 8, 2, 11, 6] are given. Remove the fewest values so that the maximum minus the minimum of the remaining values is at…
- Sorted array a = [1, 2, 4, 7, 11, 16, 22, 29]. How many index pairs i < j have a[i] + a[j] <= 20?
- How many permutations p of {0, 1, 2, 3, 4, 5} satisfy p[i] != i for every position i? Use a bitmask DP over the set of values already…