Leetcode 2491: Divide Players Into Teams of Equal Skill

grid47
grid47
Exploring patterns and algorithms
Mar 2, 2024 6 min read

You are given an array of integers representing the skill levels of players. You need to divide the players into pairs such that the sum of the skill levels of each pair is the same across all pairs. If it’s possible to form such pairs, return the sum of their chemistry, which is the product of the skill levels in each pair. If no such division is possible, return -1.
Problem
Approach
Steps
Complexity
Input: The input is a list of integers where each integer represents the skill level of a player.
Example: skill = [1, 5, 2, 4, 3, 3]
Constraints:
• 2 <= skill.length <= 100000
• skill.length is always even.
• 1 <= skill[i] <= 1000
Output: Return the sum of the chemistry for all valid pairs, or -1 if it's impossible to form the pairs.
Example: Output: 22
Constraints:
• The sum of chemistry values will be computed only for valid teams.
Goal: The goal is to check if the players can be divided into pairs with equal total skill level, and calculate the sum of their chemistry.
Steps:
• 1. Sort the skill levels of players.
• 2. Check if the sum of the first and last skill levels in the sorted array is the same for all adjacent pairs.
• 3. If the total skill sum is the same for all pairs, calculate the chemistry for each pair and return the sum. Otherwise, return -1.
Goal: The input contains an even number of players and their skill levels are positive integers between 1 and 1000.
Steps:
• skill.length is always even.
• Players' skill levels are positive integers between 1 and 1000.
Assumptions:
• Players' skill levels are always valid positive integers.
• There will always be an even number of players in the input.
Input: skill = [1, 5, 2, 4, 3, 3]
Explanation: Here, after sorting the array, the players form pairs (1, 5), (2, 4), and (3, 3) which have equal total skill (6). The chemistry is calculated as 1*5 + 2*4 + 3*3 = 5 + 8 + 9 = 22.

Input: skill = [1, 2, 3, 4]
Explanation: This input cannot form valid pairs because the total skill sum of any pair does not match, so the output is -1.

Input: skill = [3, 4]
Explanation: With only two players, they form a valid pair with a sum of 7, and the chemistry is 3*4 = 12.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus