> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Divide Players Into Teams of Equal Skill

> Tested Python solution for LeetCode 2491 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2491, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Two Pointers](/catalog/topics/two-pointers), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/divide-players-into-teams-of-equal-skill/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2491   # by problem number
lcpy gen -s divide_players_into_teams_of_equal_skill   # by problem name
```

## Problem

You are given a positive integer array `skill` of even length `n` where `skill[i]` denotes the skill of the `i<sup>th</sup>` 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.

### Examples

```
Input: skill = [3,2,5,1,3,4]
Output: 22
Explanation:
Divide the players into the following teams: (1, 5), (2, 4), (3, 3), where each team has a total skill of 6.
The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9 = 22.
```

```
Input: skill = [3,4]
Output: 12
Explanation:
The two players form a team with a total skill of 7.
The chemistry of the team is 3 * 4 = 12.
```

```
Input: skill = [1,1,2,3]
Output: -1
Explanation:
There is no way to divide the players into teams such that the total skill of each team is equal.
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/divide_players_into_teams_of_equal_skill/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/divide_players_into_teams_of_equal_skill/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def divide_players(self, skill: list[int]) -> int:
        n = len(skill)
        total = sum(skill)
        target, rem = divmod(total, n // 2)
        if rem:
            return -1
        counts = Counter(skill)
        chemistry = 0
        for val in sorted(counts):
            need = target - val
            if need < val:
                break
            if need == val:
                if counts[val] % 2:
                    return -1
                chemistry += val * val * (counts[val] // 2)
            elif counts[need] != counts[val]:
                return -1
            else:
                chemistry += val * need * counts[val]
        return chemistry
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.