Data Structures & AlgorithmsHardCodeforces Div2 D · subset sums

Problem: Two piles of stones (values [1, 6, 11, 5]) are to be divided into two groups whose sums differ as little as possible. What is the minimum possible difference? Run the subset-sum code.

Problem: Two piles of stones (values [1, 6, 11, 5]) are to be divided into two groups whose sums differ as little as possible. What is the minimum possible difference? Run the subset-sum code.
a = [1, 6, 11, 5]
S = sum(a)
reach = {0}
for x in a:
    reach |= {s + x for s in reach}
print(min(abs(S - 2 * s) for s in reach))
What is printed?
  1. A. 0
  2. B. 2
  3. C. 1
  4. D. 5

Correct answer: C. 1

The total is 23. The split 6 + 5 = 11 against 1 + 11 = 12 gives the smallest difference of 1, and 0 is impossible because 23 is odd.

More Coding Interview Problems questions

Exams that ask this topic