Data Structures & AlgorithmsVery hardFlipkart/Uber OA · placement

For the binary array [1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1], replace 0 by −1 and use prefix sums with a first-occurrence map. What is the length of the longest subarray with equal numbers of 0s and 1s, and how many such subarrays exist?

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

Correct answer: C. 6 and 12

Equal prefix sums (after 0 → −1) bound a balanced subarray; the longest uses each sum's first occurrence (6), and the count is Σ C(f, 2) over prefix-sum frequencies (12). 2·min(#0, #1) is only an upper bound.

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

Exams that ask this topic