Data Structures & AlgorithmsVery hard

When covering all distinct values of an array with a subarray, why does a sliding window track counts rather than a single flag?

  1. A. Counts are needed only to sort the values.
  2. B. A flag is always enough because values never repeat.
  3. C. A flag cannot tell when a value leaves the window, so counts are needed to know whether the window still contains every value.
  4. D. Counts are required because windows can only have length one.

Correct answer: C. A flag cannot tell when a value leaves the window, so counts are needed to know whether the window still contains every value.

When a value leaves the window its count drops, and the window stops covering the set only when that count hits zero. A boolean flag loses this information.

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

Exams that ask this topic