Leetcode Problem 2491. Divide Players Into Teams of Equal Skill
2491. Divide Players Into Teams of Equal Skill
Leetcode Solutions
Sorting and Two Pointers Approach
Sort the array of skills in ascending order.
Initialize two pointers, one at the start (i=0) and one at the end (j=skill.length-1) of the array.
Calculate the expected sum of skills for a team by adding the skill of the player at the start and the player at the end.
Initialize a variable to store the total chemistry.
Loop while the start pointer is less than the end pointer:
a. If the sum of the skills of the current pair does not match the expected sum, return -1.
b. Otherwise, calculate the product of the skills of the current pair and add it to the total chemistry.
c. Move the start pointer forward and the end pointer backward.