Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Count the subarrays of [4, 2, 2, 4, 2] whose maximum equals the minimum (single elements count).

  1. A. 1
  2. B. 15
  3. C. 3
  4. D. 6

Correct answer: D. 6

max = min means all elements are equal: the 5 single elements plus the run [2, 2], i.e. 6 (sum k(k + 1)/2 over runs of equal values). Leaving out single elements gives 1, 15 counts every subarray, and counting distinct contents {4}, {2}, {2, 2} gives 3.

More Coding Interview Problems questions

Exams that ask this topic