Ctrl K

Balanced Doubles Pairs

Medium
A coach is preparing players for a doubles table tennis tournament. Each player's level is stored in the array levels, and there is an even number of players. Split everyone into pairs so that every pair has the same combined level. The synergy of a pair is the product of its two players' levels. Return the total synergy of all pairs in such a split. If the players cannot be paired up with equal sums, return -1.
Example 1
levels=[6, 2, 4, 4, 1, 7] → 35

The pairs (1, 7), (2, 6) and (4, 4) each add up to 8. Synergy: 7 + 12 + 16 = 35.

Example 2
levels=[5, 9] → 45

Two players make one pair with synergy 5 · 9 = 45.

Example 3
levels=[1, 2, 2, 4] → -1

The weakest player (1) must team up with the strongest (4) for a sum of 5, but then 2 + 2 = 4 ≠ 5.

Constraints
  • 2 ≤ len(levels) ≤ 10⁵
  • len(levels) is even
  • 1 ≤ levels[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Subsequence of One Word Only MediumPairs With a Fixed Gap MediumTriples Hitting the Target MediumWord Growth Chain MediumRemove Pairs With a Given Sum Medium
loading Python…
Input
levels=[6, 2, 4, 4, 1, 7]
Expected
35

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