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:
2 <= skill.length <= 105
skill.length
is even.1 <= skill[i] <= 1000
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method is like trying every single combination to find the answer. We'll explore every possible team pairing to see which ones work according to the problem's rules. This means checking every single way to split up the players.
Here's how the algorithm would work step-by-step:
def divide_players_brute_force(skill):
number_of_players = len(skill)
if number_of_players % 2 != 0:
return -1
for first_player_index in range(number_of_players):
for second_player_index in range(first_player_index + 1, number_of_players):
first_team_skill = skill[first_player_index] + skill[second_player_index]
remaining_players = []
for player_index in range(number_of_players):
if player_index != first_player_index and player_index != second_player_index:
remaining_players.append(skill[player_index])
# Iterate to find a second pair with equal skill
if len(remaining_players) == 2:
if remaining_players[0] + remaining_players[1] == first_team_skill:
return first_team_skill
elif len(remaining_players) > 2:
for third_player_index in range(len(remaining_players)):
for fourth_player_index in range(third_player_index + 1, len(remaining_players)):
if remaining_players[third_player_index] + remaining_players[fourth_player_index] == first_team_skill:
return first_team_skill
# No valid division found
return -1
The key idea is to pair the players with the lowest and highest skill levels together. This ensures that each team has a balanced combined skill. Sorting the players beforehand allows us to efficiently find these pairs.
Here's how the algorithm would work step-by-step:
def divide_players_into_teams(player_skills):
player_skills.sort()
team_skill_products = []
left_index = 0
right_index = len(player_skills) - 1
# Iterate until all players are paired
while left_index < right_index:
# Pair the least skilled with the most skilled player
team_skill_products.append(player_skills[left_index] * player_skills[right_index])
# Move indices towards the center
left_index += 1
right_index -= 1
# All players must result in the same skill level
first_product = team_skill_products[0]
for product in team_skill_products:
if product != first_product:
return -1
# Return the sum of the product of skills for all teams
return sum(team_skill_products)
Case | How to Handle |
---|---|
Null input array | Throw an IllegalArgumentException or return -1 to indicate invalid input. |
Array with odd number of elements | Return -1 as an odd number of players cannot be divided into teams of two. |
Array with only two elements where the sum is not equal to the sum of other pairs. | Check if skills[0] + skills[1] maintains a consistent team skill. |
Array with elements resulting in integer overflow when multiplied | Use long data type to store the product to prevent overflow. |
Array with elements that sum to different team skills | Return -1 if there is a mismatch in team skills. |
Large input array | Ensure the sorting algorithm used has O(n log n) time complexity or the hash map approach has O(n) complexity. |
Array with duplicate pairs that result in same product, the sum of product must not be counting same pairs more than once. | Hash map or sorting helps to ensure only possible team combinations are calculated and counted only once. |
Array with negative skill values | The algorithm should correctly handle negative skill values when computing the team skill and product of skill levels. |