Data Structures & AlgorithmsHardTCS NQT · Accenture

How many contiguous subarrays of [1, 1, 2, 1, 1] have all elements of the same parity?

  1. A. 8
  2. B. 7
  3. C. 5
  4. D. 10

Correct answer: B. 7

Split the array into maximal runs of equal parity and add L(L+1)/2 for each run. The lone 2 contributes exactly one subarray.

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

Exams that ask this topic