Skip to content
BytePatterns

Closest Two-Team Split

MediumDynamic Programming#0-1-knapsack#subset-sum~30m

Problem

Players have skill ratings that are whole numbers, zero or more. Put every player on exactly one of two teams, where a team may end up empty, so that the two team totals are as close as possible. Return the smallest possible difference between the totals.

Examples

Input:  ratings = [5, 8, 13, 2]
Output: 2
Why:    13 + 2 = 15 against 5 + 8 = 13; no split reaches 14 each
Input:  ratings = [4, 9, 5]
Output: 0
Why:    9 against 4 + 5, a perfect split
Input:  ratings = [7]
Output: 7
Why:    edge case, one player means one team gets everything

Hints

0 / 3

Stuck on the idea rather than the code? Equal Split covers it.