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?

  1. A. 5
  2. B. 3
  3. C. 6
  4. 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

Exams that ask this topic