Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: How many subarrays of [2, 4, 6, 1, 3, 5] have all elements even or all elements odd?
- A. 6
- B. 21
- C. 18
- 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
- Problem: The number 9 is written in base 3 as 100. How many numbers from 1 to 100 have a base-3 representation with no digit 2?
- Problem: On a line of cells 0–9, a token starts at cell 0. Each second it moves right by 2 if the cell is even, and left by 1 if the cell…
- Problem: Count the pairs (i, j) with 1 ≤ i < j ≤ 12 for which the number i·j is a perfect square.
- Problem: The array [4, 9, 2, 7, 5, 6] is processed by insertion sort (standard insertion into the sorted prefix, shifting larger elements…
- Problem: The matrix [[1,2,3],[4,5,6],[7,8,9]] is traversed by rows, but with each row read alternately left-to-right and right-to-left…
- Problem: Find the number of pairs of indices (i, j), i < j, in the array [1, 3, 2, 4, 3, 5] such that a[i] = a[j] + 1 or a[i] = a[j] − 1.