Data Structures & AlgorithmsHard

What is the length of the longest contiguous subarray of 3 -2 0 4 -1 0 2 -5 in which the number of positive elements equals the number of negative elements?

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

Correct answer: B. 8

Map positive → +1, negative → −1 and zero → 0; the whole array has 3 positives and 3 negatives, so its sum is 0 and the answer is 8. Treating zeros as negatives gives 4, ignoring the zeros gives 6, and 3 is the number of positives.

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

Exams that ask this topic