Data Structures & AlgorithmsModerateTCS NQT · prefix sums

Problem: How many ways can [1, 2, 3, 4] be split into two non-empty contiguous parts with equal sums? Run the code.

Problem: How many ways can [1, 2, 3, 4] be split into two non-empty contiguous parts with equal sums? Run the code.
a = [1, 2, 3, 4]
print(sum(1 for k in range(1, len(a)) if sum(a[:k]) == sum(a[k:])))
What is printed?
  1. A. 1
  2. B. 0
  3. C. 2
  4. D. 3

Correct answer: B. 0

The prefix sums 1, 3, 6 never reach half of the total 10, so no split has equal sums.

More Coding Interview Problems questions

Exams that ask this topic