Data Structures & AlgorithmsHardTCS NQT · Codeforces Div2 C

For each window of 3 consecutive elements of [1, 2, 1, 3, 2, 1, 4], count the number of DISTINCT values in it. What is the sum of these counts over all windows?

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

Correct answer: C. 14

Slide the window and keep a frequency map; the number of distinct values changes only when a count moves between zero and one. Summing the window sizes instead of distinct counts gives k times the number of windows.

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

Exams that ask this topic