Data Structures & AlgorithmsVery hard

How many contiguous subarrays of [2, 5, 8, 3, 4, 7, 6, 1] of length at least 2 have consecutive elements of alternating parity (odd and even alternate throughout)?

  1. A. 36
  2. B. 21
  3. C. 7
  4. D. 28

Correct answer: D. 28

Alternation is a property of each adjacent pair inside the subarray, so it must hold across the whole window. Counting singletons, requiring length ≥ 3, or counting only adjacent pairs of the array each give a different answer.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic