Divide Players Into Teams of Equal Skill

You are given a positive integer array `skill` of even length `n`, where `skill[i]` is the skill of the `i`th player. Divide the players into `n / 2` teams of two so that every team has the same total skill. The **chemistry** of a team is the product of its two skills. Return the sum of the chemistry of all teams, or `-1` if the players cannot be divided so that every team has equal total skill.

30:00

Divide Players Into Teams of Equal Skill
medium
Free written solution
Topics
Companies

You are given a positive integer array skill of even length n, where skill[i] is the skill of the ith player. Divide the players into n / 2 teams of two so that every team has the same total skill. The chemistry of a team is the product of its two skills. Return the sum of the chemistry of all teams, or -1 if the players cannot be divided so that every team has equal total skill.

Example 1:
Input: {"skill":[3,2,5,1,3,4]}
Output: 22
Constraints:
  • 2≤skill.length≤1052 \leq \text{skill.length} \leq 10^5

  • skill.length is even.

  • 1≤skill[i]≤10001 \leq \text{skill}[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"skill":[3,2,5,1,3,4]}

6 players must form 3 teams of two with the same total. Sort first, so the weakest player can be paired with the strongest.

players as given

0

3

1

2

2

5

3

1

4

3

5

4
target = ?
teams = 0 / 3
chemistry = 0
Variables
VariableValue
n6
DepthFunction Call
Stack empty
0/9