Ctrl K

Two Equal Teams With Close Totals

Hard
You are given an integer array values of even length 2n holding the players' scores. Split the players into two teams of exactly n players each so that the teams' total scores are as close as possible. Every player joins exactly one team. Return the smallest possible absolute difference between the two teams' totals.
Example 1
values=[5, 1, 8, 2] → 2

Teams [5, 2] and [1, 8] score 7 and 9, a gap of 2. An exact 8–8 split is impossible.

Example 2
values=[-4, 10] → 14

Each team gets a single number, so the gap is |10 − (−4)| = 14.

Example 3
values=[3, -2, 6, 1, 4, 0] → 2

Teams [6, 1, -2] and [3, 4, 0] sum to 5 and 7, a gap of 2. The total is 12, but no three of the numbers add up to exactly 6.

Constraints
  • 1 ≤ n ≤ 15
  • len(values) = 2n
  • -10⁷ ≤ values[i] ≤ 10⁷
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSettle Up in the Fewest Transfers HardTwo Groups With the Same Average HardAssembling Fragments HardSquare-Sum Chains HardSmallest Team With Every Skill Hard
loading Python…
Input
values=[5, 1, 8, 2]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 89 tests on the server, hidden ones included.