The Indian Engineer

Problem 2491 Divide Players Into Teams of Equal Skill

Posted on 6 mins

Array Hash-Table Two-Pointers Sorting

Problem Statement

Link - Problem 2491

Question

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

Example 1:

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.

Example 2:

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.

Example 3:

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:

Solution

class Solution {
public:
    long long dividePlayers(vector<int>& skill) {
        sort(skill.begin(), skill.end());

        int n = skill.size();
        long long res = 0;
        int target = skill[0] + skill[n - 1];

        for (int i = 0; i < n / 2; i++) {
            int curr = skill[i] + skill[n - i - 1];

            if (curr != target) {
                return -1;
            }

            res += (long long)skill[i] * (long long)skill[n - i - 1];
        }

        return res;
    }
};

Complexity Analysis

| Algorithm                                   | Time Complexity | Space Complexity |
| ------------------------------------------- | --------------- | ---------------- |
| Modified Two-Pointer Technique with Sorting | O(n log n)      | O(1)             |

Explanation

Intial Thoughts

To approach this problem, we first need to consider how to divide the players into teams of two with equal total skills. We can start by thinking about how to pair the players in a way that each pair has the same total skill. One possible way is to sort the skills in ascending order and then pair the players from the start and end of the sorted list. This way, we can ensure that each pair has the same total skill. We also need to consider how to calculate the chemistry of each team, which is the product of the skills of the players on that team.

Intuitive Analysis

The key to solving this problem is to realize that we need to find a way to pair the players such that each pair has the same total skill. By sorting the skills in ascending order, we can pair the players from the start and end of the list, ensuring that each pair has the same total skill. We can then calculate the chemistry of each team by multiplying the skills of the players on that team. If at any point we find a pair with a different total skill, we can immediately return -1, as it is not possible to divide the players into teams with equal total skills. By following this approach, we can intuitively solve the problem and find the sum of the chemistry of all the teams.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Try sorting the skill array.

It is always optimal to pair the weakest available player with the strongest available player.


Similar Questions:

Title URL Difficulty
Minimum Moves to Equal Array Elements https://leetcode.com/problems/minimum-moves-to-equal-array-elements Medium
Max Number of K-Sum Pairs https://leetcode.com/problems/max-number-of-k-sum-pairs Medium