The pair sum of a pair (a,b)
is equal to a + b
. The maximum pair sum is the largest pair sum in a list of pairs.
(1,5)
, (2,3)
, and (4,4)
, the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8
.Given an array nums
of even length n
, pair up the elements of nums
into n / 2
pairs such that:
nums
is in exactly one pair, andReturn the minimized maximum pair sum after optimally pairing up the elements.
Example 1:
Input: nums = [3,5,2,3] Output: 7 Explanation: The elements can be paired up into pairs (3,3) and (5,2). The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
Example 2:
Input: nums = [3,5,4,2,4,6] Output: 8 Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2). The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
Constraints:
n == nums.length
2 <= n <= 105
n
is even.1 <= nums[i] <= 105
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to pair up numbers from a collection so that the largest sum from any of the pairs is as small as possible. The brute force method involves considering every single possible pairing of the numbers, no matter how inefficient it might be.
Here's how the algorithm would work step-by-step:
def minimize_maximum_pair_sum_brute_force(numbers):
import itertools
number_of_elements = len(numbers)
if number_of_elements % 2 != 0:
return -1
minimum_maximum_pair_sum = float('inf')
# Generate all possible permutations
for permutation in itertools.permutations(numbers):
maximum_pair_sum_for_permutation = 0
# Iterate through the permutation to form pairs and calculate sums
for i in range(0, number_of_elements, 2):
pair_sum = permutation[i] + permutation[i+1]
# Keep track of the maximum pair sum for this permutation
maximum_pair_sum_for_permutation = max(maximum_pair_sum_for_permutation, pair_sum)
# Compare with overall minimum maximum pair sum
minimum_maximum_pair_sum = min(minimum_maximum_pair_sum, maximum_pair_sum_for_permutation)
return minimum_maximum_pair_sum
The best way to solve this is to pair the smallest number with the largest number, the second smallest with the second largest, and so on. This strategy ensures we minimize the largest sum among all the pairs. By intelligently pairing the extremes, we avoid unnecessarily large individual pair sums that would otherwise increase the maximum value we are trying to minimize.
Here's how the algorithm would work step-by-step:
def minimize_maximum_pair_sum(numbers):
numbers.sort()
left_index = 0
right_index = len(numbers) - 1
maximum_pair_sum = 0
# Iterate until all numbers are paired
while left_index < right_index:
# Pair smallest and largest remaining numbers
current_pair_sum = numbers[left_index] + numbers[right_index]
# Update maximum_pair_sum if current pair's sum is larger
maximum_pair_sum = max(maximum_pair_sum, current_pair_sum)
left_index += 1
right_index -= 1
# The maximum pair sum among all pairs
return maximum_pair_sum
Case | How to Handle |
---|---|
Null or empty input array | Return -1 or throw an exception to indicate invalid input as no pairs can be formed. |
Array with exactly two elements | Return the sum of the two elements directly, as it's the only possible pair sum. |
Array with all identical values | The sorting approach will group identical values together, correctly pairing them to minimize the maximum pair sum. |
Array containing negative numbers | The sorting approach will handle negative numbers correctly by placing them in the appropriate order. |
Array containing zero | The sorting algorithm will handle zero correctly. |
Integer overflow in sum calculation | Use a data type with a larger range (e.g., long) to store the sum if there is a possibility of integer overflow. |
Large input array | Ensure the sorting algorithm used has a time complexity of O(n log n) to maintain efficiency. |
Array contains maximum and minimum integer values | The sorting algorithm must handle both the largest and smallest possible integers in the programming language. |