Maximum Training Pair Score
A school has an even number of students. The ability of each student is given in abilities.
Divide all students into pairs. In each pair, choose one student as the learner and the other as the helper. The helper's ability must be greater than or equal to the learner's ability.
Each pair contributes the learner's ability to the total score. Return the maximum total score that can be obtained.
Maximum Training Pair Score
Implement the following method:
long maximumTrainingPairScore(List<Integer> abilities)
abilities contains the ability score of every student.
- Every student must be used in exactly one pair.
- The method returns the maximum sum of the learner abilities.
Pairing Rules
- Each pair must contain exactly two students.
- One student in each pair must be selected as the learner.
- The other student must be selected as the helper.
helperAbility >= learnerAbility
- A pair contributes
learnerAbility to the total score.
Constraints
2 ≤ abilities.size() ≤ 100,000
abilities.size() is even.
1 ≤ abilities.get(i) ≤ 1,000,000,000
0 ≤ i < abilities.size()
- The result fits in a Java
long.
Examples
Example 1
maximumTrainingPairScore(abilities = [5, 2, 8, 6])
Output: 8
Form the pairs (2, 5) and (6, 8). The first value in each pair is the learner's ability. The total score is 2 + 6 = 8.
Example 2
maximumTrainingPairScore(abilities = [11, 4, 7, 3, 9, 5])
Output: 17
Form the pairs (3, 4), (5, 7), and (9, 11). The total score is 3 + 5 + 9 = 17.
Example 3
maximumTrainingPairScore(abilities = [6, 6, 6, 6])
Output: 12
Create two pairs with abilities (6, 6). Each pair contributes 6, so the total score is 6 + 6 = 12.
Example 4
maximumTrainingPairScore( abilities = [1, 12, 7, 4, 10, 2, 8, 5] )
Output: 22
Form the pairs (1, 2), (4, 5), (7, 8), and (10, 12). The total score is 1 + 4 + 7 + 10 = 22.