Divide Players Into Teams of Equal Skill - Problem
Divide Players Into Teams of Equal Skill
You are the coach of an esports tournament and need to organize
Given an array
1. Divide all players into exactly
2. Ensure each team has the same total skill sum
3. Calculate the chemistry of each team (product of both players' skills)
4. Return the sum of all team chemistries
If it's impossible to create teams with equal skill sums, return
Example: With skills
You are the coach of an esports tournament and need to organize
n players into balanced teams! ๐ฎGiven an array
skill of even length n where skill[i] represents the skill level of the i-th player, your task is to:1. Divide all players into exactly
n/2 teams of size 22. Ensure each team has the same total skill sum
3. Calculate the chemistry of each team (product of both players' skills)
4. Return the sum of all team chemistries
If it's impossible to create teams with equal skill sums, return
-1.Example: With skills
[3,2,5,1,3,4], you can form teams: (1,5), (2,4), (3,3). Each team sums to 6, and total chemistry = 1ร5 + 2ร4 + 3ร3 = 22. Input & Output
example_1.py โ Basic case with valid teams
$
Input:
[3,2,5,1,3,4]
โบ
Output:
22
๐ก Note:
After sorting: [1,2,3,3,4,5]. Pairs: (1,5)=6, (2,4)=6, (3,3)=6. All sums equal 6. Chemistry = 1ร5 + 2ร4 + 3ร3 = 5+8+9 = 22.
example_2.py โ Impossible case
$
Input:
[3,4]
โบ
Output:
-1
๐ก Note:
Only one possible team (3,4) with sum=7. Since there's only one team, equal sums are impossible (need at least 2 teams to compare).
example_3.py โ Simple valid case
$
Input:
[1,1,2,3]
โบ
Output:
-1
๐ก Note:
After sorting: [1,1,2,3]. Trying pairs: (1,3)=4 and (1,2)=3. Sums are different (4โ 3), so return -1.
Constraints
- 2 โค skill.length โค 105
- skill.length is even
- 1 โค skill[i] โค 1000
Visualization
Tap to expand
Understanding the Visualization
1
Sort by Skill
Arrange all players from weakest to strongest
2
Pair Extremes
Match the weakest available player with the strongest available player
3
Check Balance
Verify that all teams have the same total skill level
4
Calculate Chemistry
Sum up the chemistry (product of skills) for all valid teams
Key Takeaway
๐ฏ Key Insight: In a sorted array, if equal-sum pairs exist, pairing elements from opposite ends (smallest+largest, second-smallest+second-largest, etc.) always produces the optimal solution with O(n log n) time complexity.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code