Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7] contain exactly 2 odd numbers?
- A. 8
- B. 10
- C. 12
- D. 15
Correct answer: A. 8
Track the number of odd values with a prefix count; a subarray with exactly k odds corresponds to a pair of prefixes whose odd counts differ by k.
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 index pairs i < j in [1, 2, 3, 4, 5] have a[i] XOR a[j] equal to a power of two?
- How many index triples i < j < k in the array [1, 2, 3, 4, 5, 6] have a[i] + a[j] + a[k] = 10?
- How many values in [4, 4, 5, 6, 6, 7, 8] occur exactly once in the array?
- How many contiguous subarrays of [1, 1, 2, 2, 2, 3] contain only one distinct value?
- How many index pairs i < j in [1, 4, 7, 2, 5, 8] satisfy that a[i] - a[j] is divisible by 3?
- How many substrings of the string aaabaa consist only of the letter a?