Data Structures & AlgorithmsHard

How many contiguous subarrays of [1, 2, 3, 1, 2, 1, 3, 3] contain the values 1, 2 and 3 each at least once?

  1. A. 4
  2. B. 19
  3. C. 27
  4. D. 17

Correct answer: B. 19

A two-pointer window that counts how many of each of 1, 2, 3 it contains gives, for each right end, the number of left starts that keep all three present. Restricting to length 3, counting any two values, or taking the complement each give different totals.

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

Exams that ask this topic