Data Structures & AlgorithmsVery hard
How many contiguous subarrays of 1 2 3 4 5 6 7 8 of length at least 2 contain EXACTLY as many odd values as even values?
- A. 6
- B. 17
- C. 16
- D. 15
Correct answer: C. 16
Map odd to +1 and even to −1; equal counts give zero balance, which is the prefix-sum counting trick again. Including singletons (which can never balance), testing balance one, or testing length parity each count different sets.
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
- What is the length of the shortest contiguous subarray of 1 4 6 3 8 5 2 7 that contains at least 2 odd values AND at least 2 even values?
- How many index pairs i < j in 2 3 1 4 5 2 6 have the closed block A[i..j] strictly increasing (with j > i)?
- How many contiguous windows of length exactly 3 in 4 1 2 3 9 8 6 7 are strictly monotone (increasing or decreasing) AND contain the global…
- Choose two NON-OVERLAPPING non-empty contiguous subarrays of 2 -3 5 1 -2 4 -1 3, one entirely to the left of the other. What is the…
- What is the length of the longest contiguous subarray of 2 0 1 3 1 1 2 0 in which the sum of (element − 1) over the subarray equals 0?
- How many positions i with 1 ≤ i ≤ n−2 in 5 3 6 2 7 4 8 1 9 are STRICT local minima (strictly smaller than both neighbours)?