Sponsored
Sponsored
Sort the array first. Then, use a two-pointer technique to pick the smallest and largest elements to form pairs. Start with the smallest, pair it with the largest, and move inwards. This minimizes the largest sum in each pair.
The time complexity is O(n log n) due to the sorting step, and the space complexity is O(1) as we don't use any additional data structures.
1def minPairSum(nums):
2 nums.sort()
3 max_sum = 0
4 for i in range(len(nums) // 2):
5 pair_sum = nums[i] + nums[-1-i]
6 if pair_sum > max_sum:
7 max_sum = pair_sum
8 return max_sum
9
10# Testing the function
11test_nums = [3, 5, 2, 3]
12print('Output:', minPairSum(test_nums)) # Output: 7
The Python solution leverages the built-in sort()
method to sort the list, then uses a loop to calculate the pair sums and determine the maximum.
Use the sorted array but pair elements directly based on their index positions in a staggered manner. This approach takes advantage of the sorted order to pair elements directly based on their index positions.
The time complexity remains O(n log n) due to sorting, and the space complexity is O(1).
1
The Python implementation uses index-based pairing on a sorted list to determine the minimal maximum pair sum.