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

What is the length of the longest contiguous subarray of [1, 2, 1, 3, 4, 3, 5] in which at most one element is a repeat of an earlier element (counting extra copies)?

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

Correct answer: A. 6

Shrink the window while it holds more than one extra copy: [2, 1, 3, 4, 3, 5] has one extra 3 and length 6. The whole array (7) has two extra copies (1 and 3), 2 counts those extra copies, and 1 is k itself.

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

Exams that ask this topic