Divide Players Into Teams of Equal Skill (LeetCode 2491): Solution and Explanation
Split players into pairs that all have the same total skill and return the sum of the products of each pair, or -1 if that is impossible.
Intuition
Every team must sum to the same value T, and since there are n/2 teams, T is the total skill divided by n/2.
The weakest player needs a partner worth T minus its skill. If anyone stronger than the strongest player existed it would be needed, so the strongest player is the only one that can complete the weakest player's team. That fixes T = min + max.
Removing that pair leaves the same problem on the rest, so after sorting, team i is skill[i] with skill[n - 1 - i]. One wrong sum means no split works.
Approach: Sort and pair from both ends
- Sort skill.
- Set target = skill[0] + skill[n - 1].
- For i from 0 to n/2 - 1, let a = skill[i] and b = skill[n - 1 - i].
- If a + b != target, return -1.
- Otherwise add a * b to a 64-bit chemistry total.
- Return chemistry.
Step-by-step example
skill = [3,2,5,1,3,4]
- Sorted: [1,2,3,3,4,5]. target = 1 + 5 = 6.
- Team 0: 1 + 5 = 6, chemistry 5.
- Team 1: 2 + 4 = 6, chemistry 5 + 8 = 13.
- Team 2: 3 + 3 = 6, chemistry 13 + 9 = 22.
- All teams matched, return 22.
Code: Python, Java, C++
1class Solution:
2 def dividePlayers(self, skill: List[int]) -> int:
3 skill.sort()
4 n = len(skill)
5 target = skill[0] + skill[-1]
6 chemistry = 0
7 for i in range(n // 2):
8 a, b = skill[i], skill[n - 1 - i]
9 if a + b != target:
10 return -1
11 chemistry += a * b
12 return chemistry1class Solution {
2 public long dividePlayers(int[] skill) {
3 Arrays.sort(skill);
4 int n = skill.length;
5 int target = skill[0] + skill[n - 1];
6 long chemistry = 0;
7 for (int i = 0; i < n / 2; i++) {
8 int a = skill[i], b = skill[n - 1 - i];
9 if (a + b != target) {
10 return -1;
11 }
12 chemistry += (long) a * b;
13 }
14 return chemistry;
15 }
16}1class Solution {
2public:
3 long long dividePlayers(vector<int>& skill) {
4 sort(skill.begin(), skill.end());
5 int n = skill.size();
6 int target = skill[0] + skill[n - 1];
7 long long chemistry = 0;
8 for (int i = 0; i < n / 2; i++) {
9 int a = skill[i], b = skill[n - 1 - i];
10 if (a + b != target) {
11 return -1;
12 }
13 chemistry += (long long) a * b;
14 }
15 return chemistry;
16 }
17};Time and space complexity
O(n log n)
Sorting dominates; the pairing pass is O(n).
O(1)
Only a few variables beyond the in-place sort.
Other approaches
Counting with a frequency table (time O(n + V), space O(V))
Compute target = 2 * sum / n (return -1 if not an integer). Count skills in an array of size 1001; for each skill s, it must be matched with an available target - s. Avoids sorting.
Common mistakes
- Accumulating chemistry in a 32-bit int. With 10^5 players of skill up to 1000 the total can reach about 2.5 * 10^10.
- Computing a * b in 32 bits before widening it; cast one factor to long first in Java, C++ and C#.
- Only checking that the total is divisible by n/2. [1,1,2,4] has total 8 and target 4, but 1 + 4 = 5, so it still fails.
- Stopping after the first mismatch is correct, but returning the partial chemistry instead of -1 is not.
Frequently asked questions
Why must the smallest player pair with the largest?
The common team total T is at least min + max: the team containing the max sums to at least max + min. It is also at most min + max: the team containing the min sums to at most min + max. So T = min + max and the min's partner must be exactly the max.
Why return a 64-bit value?
Each product is at most 10^6 and there are up to 5 * 10^4 teams, so the sum can exceed 2^31 - 1. Java, C++, C# and Go return long, long long or int64 here.
Can I avoid sorting?
Yes. The target is 2 * sum / n, and a counting array over skills 1 to 1000 lets you match each player with target - skill in linear time.