Data Structures & AlgorithmsModerate
In the array [1, 2, 1, 3, 2, 1, 4, 5], how many contiguous subarrays contain exactly 2 odd numbers?
- A. 24
- B. 7
- C. 9
- D. 10
Correct answer: C. 9
Exactly k equals at-most-k minus at-most-(k−1), which is the standard difference of two sliding-window counts. Using at-most-k alone includes subarrays with fewer odd numbers, and the difference (k+1) minus k counts exactly k+1 odd numbers.
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 [10, 5, 2, 6] have a product strictly less than 100? Every value is a positive integer.
- What is the sum of the sums of all contiguous subarrays of [999999999, 888888888, 777777777], taken modulo 1000000007?
- What is the sum, over all contiguous subarrays of [3, 1, 3, 2], of the maximum element of each subarray?
- In [4, 7, 5, 9, 6, 3], how many contiguous subarrays have (max − min) ≤ 3?
- How many contiguous subarrays of [1, 2, 1, 3, 3, 2, 2, 1] contain at most 2 distinct values?
- For the array [1, 3, 2, 6, 7, 4, 8, 12, 5], how many index pairs i < j have (J[i] + J[j]) divisible by 4?