Skip to main content
Back to problems
Leetcode
Medium
Arrays
Sorting
Hash Maps
Math
Amazon
Divide Players Into Teams Of Equal Skill

Pair up all players so every team of two has the same total skill, then sum the chemistry across teams.

Acceptance 0%
Problem Statement

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 * b for 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.length is 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 -1 if no valid partition exists.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.