Data Structures & AlgorithmsVery hardCodeforces Div2 B-D · ICPC regional

What is the length of the longest contiguous subarray of [1, 2, 1, 2, 3, 2, 2, 4, 1, 1, 1] that contains at most 2 distinct values?

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

Correct answer: A. 4

Shrink the window from the left whenever the distinct count exceeds k. Allowing 3 distinct values (k = 3) is the trap for those who forget the limit.

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

Exams that ask this topic