Data Structures & AlgorithmsVery hard

In the 'longest window with equal numbers of high and low values' problem, what turns the condition into a standard prefix-sum problem?

  1. A. Sort the values and compare the halves.
  2. B. Count the high values and divide by two.
  3. C. Assign +1 to one class and −1 to the other, so equal counts become a zero-sum subarray.
  4. D. Remove the middle value.

Correct answer: C. Assign +1 to one class and −1 to the other, so equal counts become a zero-sum subarray.

The balance trick reduces 'equal counts' to 'sum zero', and then first-occurrence prefix hashing finds the longest such block in linear time.

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

Exams that ask this topic