#2491

Divide Players Into Teams of Equal Skill

pupil · 575 · lc medium +29 · verified · 68.9% accepted · 1,080 likes · top 76%

Description

Given an even-length integer array skill where skill[i] is the skill level of player i, pair 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 players' skills. Return the sum of chemistry across all teams, or -1 if an equal-skill pairing is impossible.

Example 1:

Input: skill = [3,2,5,1,3,4]
Output: 22
Explanation:
Divide the players into the following teams: (1, 5), (2, 4), (3, 3), where each team has a total skill of 6.
The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9 = 22.

Example 2:

Input: skill = [3,4]
Output: 12
Explanation:
The two players form a team with a total skill of 7.
The chemistry of the team is 3 * 4 = 12.

Example 3:

Input: skill = [1,1,2,3]
Output: -1
Explanation:
There is no way to divide the players into teams such that the total skill of each team is equal.

Code

1
2
3