Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: How many subarrays of [2, 4, 6, 1, 3, 5] have all elements even or all elements odd?

  1. A. 6
  2. B. 21
  3. C. 18
  4. D. 12

Correct answer: D. 12

The maximal uniform runs are [2,4,6] and [1,3,5], each giving 3·4/2 = 6 subarrays, total 12. Leaving out single elements gives 6, all subarrays number 21, and using k² per run gives 18.

More Coding Interview Problems questions

Exams that ask this topic