MediumArrayHash TableTwo PointersSortingUpdated Sep 2026

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

sorted skillall players123345012345Sorting reveals which players can be partnerspairs1+52+43+3012Every pair sums to 6, so the split is valid
Sort the skills, then the smallest must team up with the largest so every pair hits the same total.

Constraints

  • ●2 <= skill.length <= 10^5
  • ●skill.length is even
  • ●1 <= skill[i] <= 1000

Examples

Example 1
Inputskill = [3,2,5,1,3,4]
Output22
WhyThe teams (1,5), (2,4) and (3,3) all add up to 6, so the split is valid. Their products are 5, 8 and 9, and 5 + 8 + 9 = 22.
Example 2
Inputskill = [3,4]
Output12
WhyThere is only one team, so the equal-sum rule is automatically satisfied. Its chemistry is 3 * 4 = 12.
Example 3
Inputskill = [1,1,2,3]
Output-1
WhyThe total skill is 7, and each of the 2 teams would need 3.5, which is impossible with whole numbers. No valid split exists.

Asked At

1 companies
CompanyDifficulty
VisaMediumView all Visa questions →

How to Think About It

1

Every team must sum to total / (n / 2). If that is not an integer, the answer is -1.

2

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.

3

Check each pair sums to the same target; if any pair differs, return -1.

4

Accumulate the product skill[i] * skill[n-1-i] for each pair, using 64-bit arithmetic.

5

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

sorted skill123345012345ijStart: i at the smallest, j at the largest. Target = 1+ 5 = 6sorted skill123345012345ij2 + 4 = 6 and 3 + 3 = 6, so all pairs match the targettarget = skill[0] + skill[n-1]res += skill[i] * skill[n-1-i]1*5 + 2*4 + 3*3 = 22
Walk one pointer from the left and one from the right. Each pair must match the target sum.
ComplexityTime O(n log n)Space O(1)Sorting dominates the cost; the pairing loop is a single linear pass with no extra storage.

What Trips People Up in Real Interviews

1

Trying all pairings with backtracking — exponential and unnecessary.

2

Returning the common team sum instead of the sum of products.

3

32-bit overflow when summing up to 5 * 10^4 products of values up to 1000.

4

Not justifying why smallest-with-largest is forced.

Solution Code

O(n log n) time
def 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 res

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently 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.