Pair up all players so every team of two has the same total skill, then sum the chemistry across teams.
Divide Players Into Teams of Equal Skill
You are given an array skill where skill[i] is the skill of the i-th player.
Your task is to divide all players into teams of exactly two players such that every team has the same total skill. If this is impossible, return -1.
If the players can be divided successfully, the chemistry of a team is the product of the two players' skills. Return the sum of the chemistry of all teams.
In other words:
- Every player must belong to exactly one pair.
- All pairs must have the same sum.
- If a valid pairing exists, compute the total of
a * bfor every pair(a, b).
This problem is mainly about recognizing whether a global pairing constraint is feasible and then efficiently validating it with counting or sorting.
Input Format
- A single integer array
skill. skill.lengthis even.- Each value represents one player's skill.
Output Format
- Return the total chemistry if valid pairings exist.
- Otherwise return
-1.
Constraints
- The number of players is even.
- Each player must be used exactly once.
- All teams must have the same combined skill.
- Return
-1if no valid partition exists.
Example 1
Input
skill = [3,2,5,1,3,4]
Output
22
Explanation
One valid partition is (1, 5), (2, 4), and (3, 3). Each pair sums to 6. The total chemistry is 15 + 24 + 3*3 = 5 + 8 + 9 = 22.
Example 2
Input
skill = [1,1,2,3]
Output
-1
Explanation
The total sum is 7, so it cannot be split into pairs with the same sum.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.