Divide Players Into Teams of Equal Skill - Problem
You are given a positive integer array skill of even length n where skill[i] denotes the skill of the ith 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.
Input & Output
Example 1 — Basic Valid Case
$
Input:
skill = [3,2,5,1,3,4]
›
Output:
22
💡 Note:
Sort to [1,2,3,3,4,5]. Pair from ends: (1,5) sum=6 chemistry=5, (2,4) sum=6 chemistry=8, (3,3) sum=6 chemistry=9. Total: 5+8+9=22
Example 2 — Impossible Case
$
Input:
skill = [3,4]
›
Output:
12
💡 Note:
Only one possible pairing: (3,4). Team sum=7, chemistry=3×4=12. Since there's only one team, all teams have equal sums.
Example 3 — Cannot Form Equal Teams
$
Input:
skill = [1,1,2,3]
›
Output:
-1
💡 Note:
Sort to [1,1,2,3]. Try pairing from ends: (1,3) sum=4, (1,2) sum=3. Team sums are not equal, so return -1.
Constraints
- 2 ≤ skill.length ≤ 105
- skill.length is even
- 1 ≤ skill[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of player skills: [3,2,5,1,3,4]
2
Process
Sort and pair optimally: (1,5), (2,4), (3,3)
3
Output
Sum of team chemistry: 5+8+9=22
Key Takeaway
🎯 Key Insight: Sorting enables optimal pairing from opposite ends - this guarantees equal team sums if mathematically possible
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code