Data Structures & AlgorithmsHardTCS NQT · Codeforces Div2 B

How many contiguous subarrays of [1, 2, 1, 3, 4, 2, 5] of length at least 2 consist of pairwise distinct values?

  1. A. 16
  2. B. 11
  3. C. 14
  4. D. 6

Correct answer: C. 14

For each left end, the valid right ends extend until the first repeated value appears, so a sliding window with the 'last seen' positions counts them in O(n). Counting subarrays of length exactly 2 with distinct values misses the longer ones.

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

Exams that ask this topic