Data Structures & AlgorithmsVery hard

How many contiguous subarrays of [1, 2, 1, 3, 2, 1, 4, 2, 3] contain each of the values 1, 2, ..., L exactly once, where L is the subarray length? (Such a subarray is a permutation of 1..L.)

  1. A. 12
  2. B. 25
  3. C. 8
  4. D. 11

Correct answer: D. 11

A subarray is a permutation of 1..L exactly when its sorted values are 1..L, which checks the value set and the length together. Testing only the maximum equals the length, or only distinctness, admits subarrays that miss some value.

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

Exams that ask this topic