Given an integer array nums
of 2n
integers, group these integers into n
pairs (a1, b1), (a2, b2), ..., (an, bn)
such that the sum of min(ai, bi)
for all i
is maximized. Return the maximized sum.
Example 1:
Input: nums = [1,4,3,2] Output: 4 Explanation: All possible pairings (ignoring the ordering of elements) are: 1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3 2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3 3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4 So the maximum possible sum is 4.
Example 2:
Input: nums = [6,2,6,5,1,2] Output: 9 Explanation: The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.
Constraints:
1 <= n <= 104
nums.length == 2 * n
-104 <= nums[i] <= 104
The problem asks us to find the maximum sum of minimum values among all possible pairings of numbers in a given array nums
of even length. The key insight is that to maximize this sum, we need to pair the smallest numbers with each other, the next smallest pair together, and so on. This is because the minimum of any pair will contribute to the final sum, and we want to minimize the loss by pairing similar values.
Algorithm:
Sort the array: The most efficient approach involves sorting the input array nums
in ascending order. This allows us to easily create pairs of the smallest numbers.
Pair and sum: After sorting, we can iterate through the sorted array, taking every other element (starting from the first element) to obtain the minimum values from each pair. These minimum values are then added to a running sum.
Return the sum: The final sum represents the maximized sum of minimum values among all possible pairings.
Time and Space Complexity:
Time Complexity: The dominant operation is sorting the array, which takes O(n log n) time, where n is the length of the array. The subsequent iteration and summation take O(n) time, which is dominated by the sorting complexity. Therefore, the overall time complexity is O(n log n).
Space Complexity: The sorting algorithm used might require extra space depending on the implementation (e.g., merge sort might use O(n) space, while in-place algorithms like heapsort might use O(1) space). In most cases, the space used for the running sum is negligible compared to the sorting space. Thus, the space complexity is O(log n) for efficient sorting algorithms or O(n) for less efficient ones. The solution presented uses in-place sorting in many of the implementations, hence the O(log n) space in those.
Code Examples:
The code examples below implement the algorithm in different programming languages. They all follow the same basic structure: sort the input array, iterate through the sorted array selecting every other element (minimum of each pair), and calculate the sum.
Python:
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
return sum(nums[::2])
Java:
import java.util.Arrays;
class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int sum = 0;
for (int i = 0; i < nums.length; i += 2) {
sum += nums[i];
}
return sum;
}
}
C++:
#include <algorithm>
#include <vector>
class Solution {
public:
int arrayPairSum(std::vector<int>& nums) {
std::sort(nums.begin(), nums.end());
int sum = 0;
for (size_t i = 0; i < nums.size(); i += 2) {
sum += nums[i];
}
return sum;
}
};
JavaScript:
var arrayPairSum = function(nums) {
nums.sort((a, b) => a - b);
let sum = 0;
for (let i = 0; i < nums.length; i += 2) {
sum += nums[i];
}
return sum;
};
These code examples all demonstrate the same fundamental approach with minor syntactic variations. They effectively solve the problem with optimal time and space complexity (for those implementations utilizing efficient sorting).