Data Structures & AlgorithmsModerate

What is the length of the shortest contiguous subarray of 2 1 2 3 1 3 2 1 that contains EVERY distinct value appearing in the array?

  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 5

Correct answer: A. 3

Use a sliding window with a count map: expand right until all values are covered, then shrink left while coverage holds. Counting distinct values is the lower bound, not the window length.

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

Exams that ask this topic