Data Structures & AlgorithmsVery hard
How many contiguous subarrays of [2, 5, 8, 3, 4, 7, 6, 1] of length at least 2 have consecutive elements of alternating parity (odd and even alternate throughout)?
- A. 36
- B. 21
- C. 7
- D. 28
Correct answer: D. 28
Alternation is a property of each adjacent pair inside the subarray, so it must hold across the whole window. Counting singletons, requiring length ≥ 3, or counting only adjacent pairs of the array each give a different answer.
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 positive integers [2, 1, 4, 3, 1, 2] have a sum at most 6?
- How many contiguous subarrays of [2, 4, 1, 3, 5, 2] have a sum in the closed range [5, 9]?
- In the array [2, 3, 1, 2, 4, 3] (all positive), what is the length of the shortest contiguous subarray whose sum is at least 7?
- How many contiguous subarrays of [1, 5, 2, 8, 3, 7] have a sum divisible by their own length?
- For [2, 3, 4, 6, 9, 5, 12], how many index pairs i < j have the product BB[i]·BB[j] divisible by 6?
- How many contiguous subarrays of [5, 5, 3, 3, 6, 6, 6] have XOR equal to 0?