aboutsummaryrefslogtreecommitdiff
path: root/min_max_pair_arr.py
blob: 1a7dba3e8d2713be43d40c676aa7cdbfaef3be82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 1877. Minimize Maximum Pair Sum in Array

"""
the pair sum of pair '(a, b)' is equal to 'a + b'. the maximum pair sum is
the largest pair sum in a list of pairs. given an array 'nums' of even length
'n', pair up the lements of 'nums' into 'n / 2' pairs such that each element
of 'nums' is in exactly one pair and the maximum pair sum is minimised.
return the minimised maximium pair sum after optimally pairing up the
elements.
"""


class Solution(object):
    def minPairSum(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return max(a + b for a, b in zip(sorted(nums), sorted(nums)[::-1]))


if __name__ == "__main__":
    obj = Solution()
    print(obj.minPairSum([3, 5, 2, 3]))  # expect: 7
    print(obj.minPairSum([3, 5, 4, 2, 4, 6]))  # expect: 8