2491. Divide Players Into Teams of Equal Skill
Asked at Visa
Difficulty
Medium
Time
O(n log n)
Space
O(1)
Pattern
Array
Companies
1
Problem
You are given an array skill of even length n. Each value is the skill of one player. You must split all players into n / 2 teams, and every team must have exactly 2 players.
The rule is that every team must have the same total skill. The total skill of a team is the sum of its two players' skills. If you cannot make all teams equal this way, return -1.
The chemistry of a team is the product of its two players' skills. Return the sum of the chemistry of all teams, or -1 if no valid split exists.
Pair everyone so each pair sums the same
Constraints
- ●
2 <= skill.length <= 10^5 - ●
skill.length is even - ●
1 <= skill[i] <= 1000
Examples
skill = [3,2,5,1,3,4]22skill = [3,4]12skill = [1,1,2,3]-1Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Visa | Medium | View all Visa questions → |
How to Think About It
Every team must sum to total / (n / 2). If that is not an integer, the answer is -1.
Key insight: after sorting, the smallest player must pair with the largest (anyone else would give a different sum from the pairs that remain). So pair from both ends inward.
Check each pair sums to the same target; if any pair differs, return -1.
Accumulate the product skill[i] * skill[n-1-i] for each pair, using 64-bit arithmetic.
Walkthrough for [3,2,5,1,3,4]: sorted [1,2,3,3,4,5]; pairs (1,5), (2,4), (3,3) all sum to 6 -> 5 + 8 + 9 = 22.
Optimal Approach
Step 1: Sort skill.
Step 2: target = skill[0] + skill[-1], res = 0.
Step 3: For i in 0..n/2-1: if skill[i] + skill[n-1-i] != target, return -1; else res += skill[i] * skill[n-1-i].
Step 4: Return res.
Time: O(n log n) (or O(n) with counting since skills are small). Space: O(1) extra.
Two pointers from both ends
What Trips People Up in Real Interviews
Trying all pairings with backtracking — exponential and unnecessary.
Returning the common team sum instead of the sum of products.
32-bit overflow when summing up to 5 * 10^4 products of values up to 1000.
Not justifying why smallest-with-largest is forced.
Solution Code
O(n log n) timedef dividePlayers(skill):
skill.sort()
n = len(skill)
target = skill[0] + skill[-1]
res = 0
for i in range(n // 2):
a, b = skill[i], skill[n - 1 - i]
if a + b != target:
return -1
res += a * b
return resFrequently Asked Questions
What is the Divide Players Into Teams of Equal Skill problem?
Divide Players Into Teams of Equal Skill asks you to pair up an even number of players so that every pair has the same total skill, and to return the sum of the products of each pair's skills (or -1 if impossible). Sorting shows the only possible pairing: smallest with largest.
How do you solve Divide Players Into Teams of Equal Skill?
The optimal approach runs in O(n log n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Divide Players Into Teams of Equal Skill?
Divide Players Into Teams of Equal Skill is asked at Visa. It is a medium difficulty problem.
What are common mistakes on Divide Players Into Teams of Equal Skill?
- Trying all pairings with backtracking — exponential and unnecessary.
- Returning the common team sum instead of the sum of products.
- 32-bit overflow when summing up to `5 * 10^4` products of values up to 1000.
- Not justifying why smallest-with-largest is forced.