Divide Players Into Teams of Equal Skill

You are given a positive integer array skill of even length n where skill[i] denotes the skill of the i^th player. Divide the players into n / 2 teams of size 2 such that the total skill of each team is equal.

The chemistry of a team is equal to the product of the skills of the players on that team.

Return the sum of the chemistry of all the teams, or return -1 if there is no way to divide the players into teams such that the total skill of each team is equal.

Example 1
Inputskill = [3,2,5,1,3,4]
Output22
Teams can be formed as (1, 5), (2, 4), and (3, 3), each with total skill 6, and their chemistry sum is 22.
Example 2
Inputskill = [3,4]
Output12
The two players form one team with chemistry 3 * 4 = 12.

Constraints

  • 2 <= skill.length <= 10^5
  • skill.length is even.
  • 1 <= skill[i] <= 1000

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate