Data Structures & AlgorithmsVery hardCodeforces Div2 B-D · ICPC regional

What is the length of the longest contiguous subarray of [0, 1, 0, 0, 1, 1, 0, 1] containing equal numbers of 0s and 1s?

  1. A. 8
  2. B. 6
  3. C. 4
  4. D. 2

Correct answer: A. 8

Map 0 to −1 and store the first index of each prefix sum, seeding prefix 0 at index −1. The whole array has four 0s and four 1s, so the answer is 8. Without the seed, spans starting at index 0 are missed and the best is 6; 4 counts the zeros.

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

Exams that ask this topic