1272. Even Pair Split
A tournament organiser has an even number of players, each with a skill rating, and the ratings are already listed in non-decreasing order in nums. She wants to split all players into teams of exactly two so that every team has the same combined rating. Return true if such a split exists and false otherwise. Every player belongs to exactly one team.
Because the list is sorted, the weakest player can only be matched with the strongest one if a uniform sum is possible, and the same reasoning applies to the remaining players. Use two pointers that start at both ends and move towards each other, comparing each pair sum with the first one. Sums can exceed the 32-bit range, so use 64-bit arithmetic. Aim for O(n) time and O(1) extra space.
Example 1
- Input:
- nums = [1,2,3,4]
- Output:
- true
- Explanation:
The pairs (1, 4) and (2, 3) both sum to 5, so a uniform split exists.
Example 2
- Input:
- nums = [1,2,4,4]
- Output:
- false
- Explanation:
The outer pair sums to 5 but the inner pair sums to 6, so no uniform split exists.
Example 3
- Input:
- nums = [-4,-1,1,4]
- Output:
- true
- Explanation:
Both (-4, 4) and (-1, 1) sum to 0, so the answer is true.
Constraints
2 ≤ nums.length ≤ 1000
nums.length is even
-109 ≤ nums[i] ≤ 109
nums is sorted in non-decreasing order
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)
- Space
- O(1)