Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS

The numbers [31, 26, 33, 21, 40] are split into two groups (every number goes to exactly one group). What is the smallest possible absolute difference between the two group sums?

  1. A. 19
  2. B. 1
  3. C. 7
  4. D. 5

Correct answer: D. 5

Find all achievable subset sums s, then minimise |total − 2s|; this is the subset-sum DP. A greedy split (always put the next largest number in the lighter group) is a good heuristic but is not always optimal.

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

Exams that ask this topic