Data Structures & AlgorithmsModerate

How many contiguous subarrays of [1, 2, 3, 3, 2, 1, 4, 4] have even length and equal sums in their left half and right half?

  1. A. 5
  2. B. 3
  3. C. 12
  4. D. 16

Correct answer: A. 5

For an even-length block the halves are equal-length, so compare the sum of the first half with the sum of the second half directly. Skipping the middle element on odd lengths or testing the ends only changes the set of qualifying blocks.

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

Exams that ask this topic