Data Structures & AlgorithmsHard
In the binary array [1, 0, 0, 1, 0, 1, 1, 0, 0, 0], how many contiguous subarrays contain equal numbers of 0s and 1s?
- A. 10
- B. 14
- C. 4
- D. 15
Correct answer: B. 14
Map 1 to +1 and 0 to −1; equal counts means a zero-sum subarray, which is a pair of equal prefix sums. Forgetting the prefix 0 at the start misses subarrays beginning at index 0, while counting prefixes instead of pairs gives a different number.
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 longest contiguous subarray of [2, -4, 3, 1, 6, -2, 5, 4] whose sum is divisible by 6?
- How many contiguous subarrays of [1, 2, 1, 3, 4, 2, 3] contain no repeated value?
- Count the inversions of [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]: pairs of positions i < j with N[i] > N[j]. Equal values are not inversions.
- How many indices i satisfy: the sum of the elements before i equals the sum of the elements after i, excluding P2[i] itself? (P2 = [1, 2…
- How many DISTINCT values appear among the sums of all non-empty contiguous subarrays of [1, 2, 3, 1, 2, 3]?
- How many contiguous subarrays of [6, 10, 15, 4, 9] have gcd equal to 1?