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.)
- A. 12
- B. 25
- C. 8
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many contiguous subarrays of [1, 2, 3, 3, 2, 1, 4, 4] have even length and equal sums in their left half and right half?
- How many contiguous subarrays of [3, 1, 4, 1, 5, 9, 2, 6] have a sum that is divisible by their own maximum element?
- For the array [4, -1, 2, -3, 5, -6, 3], what is the maximum value of (sum of a non-empty contiguous subarray) − 2 × (its length)?
- How many index pairs i < j in [5, 1, 9, 4, 8, 3, 7] satisfy |A[i] − A[j]| ≤ 3?
- How many contiguous subarrays of [1, 0, 1, 1, 0, 0, 1, 0, 1] contain EXACTLY one 0?
- How many contiguous subarrays of [1, 2, 3, 1, 2, 1, 3, 3] contain the values 1, 2 and 3 each at least once?