-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.cpp
27 lines (24 loc) · 861 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution
{
public:
long long dividePlayers(vector<int> &skill)
{
// Step 1: Sort the skill array
sort(skill.begin(), skill.end());
int n = skill.size();
int totalSkill = skill[0] + skill[n - 1]; // Required sum for each pair
long long chemistrySum = 0;
// Step 2: Pair players using two pointers
for (int i = 0; i < n / 2; i++)
{
// Check if the sum of current pair matches the required totalSkill
if (skill[i] + skill[n - i - 1] != totalSkill)
{
return -1; // Invalid configuration, return -1
}
// Calculate the chemistry (product of pair) and add it to the sum
chemistrySum += (long long)skill[i] * skill[n - i - 1];
}
return chemistrySum; // Return total chemistry
}
};