1292. Paired Above Target
A coach is forming doubles teams from a squad. The array nums holds the rating of each player, in no particular order, and a pair is strong when the two ratings add up to at least target. Every player can belong to at most one pair. Return the largest number of disjoint strong pairs that can be formed. Players left over simply sit out. Sums can exceed the 32-bit range, so add with 64-bit integers.
Aim for O(n log n) time and O(n) space, where n is the length of nums: sort, then walk converging pointers from both ends.
Example 1
- Input:
- nums = [3,1,4,2], target = 5
- Output:
- 2
- Explanation:
Sorted, the ratings are 1, 2, 3, 4. Pairing 1 with 4 and 2 with 3 gives two pairs that each add up to 5.
Example 2
- Input:
- nums = [1,1,1], target = 3
- Output:
- 0
- Explanation:
The best sum of any two ratings is 2, which is below 3, so no pair is strong.
Example 3
- Input:
- nums = [9,9,9,9], target = 18
- Output:
- 2
- Explanation:
Any two players add up to 18, so the four players form two pairs.
Constraints
1 ≤ nums.length ≤ 1000
0 ≤ nums[i] ≤ 109
0 ≤ target ≤ 2 * 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n log n)
- Space
- O(n)