Data Structures & AlgorithmsModerateTCS NQT · Codeforces Div2 B

How many contiguous subarrays of the binary array [1, 0, 1, 1, 0, 0, 1, 0] contain equally many 0s and 1s? (Non-empty subarrays only.)

  1. A. 14
  2. B. 12
  3. C. 11
  4. D. 4

Correct answer: B. 12

Map 1 to +1 and 0 to −1; a subarray has equal counts exactly when its prefix sums are equal, so count equal pairs of prefix sums with a hash map.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic