Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many contiguous subarrays of [1, 2, 1, 3, 4, 2, 3] contain exactly 3 distinct values?

  1. A. 7
  2. B. 6
  3. C. 20
  4. D. 14

Correct answer: B. 6

Exactly-3 = atMost(3) − atMost(2) = 20 − 14 = 6. Stopping at atMost(3) gives 20, atMost(2) gives 14, and 7 is the count for exactly 2 distinct values.

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

Exams that ask this topic