Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

How many contiguous subarrays of [1, 2, 1, 3, 2, 2, 1] contain at most two distinct values?

  1. A. 17
  2. B. 15
  3. C. 19
  4. D. 16

Correct answer: D. 16

Count windows with at most two distinct values using a sliding window that counts, for each right end, the valid left ends. Only the subarrays containing 3 are excluded when they have a third value.

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

Exams that ask this topic