Skip to content

1877. Minimize Maximum Pair Sum in Array 👍

  • Time: $O(\texttt{sort})$
  • Space: $O(\texttt{sort})$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
 public:
  int minPairSum(vector<int>& nums) {
    int ans = 0;

    ranges::sort(nums);

    for (int i = 0, j = nums.size() - 1; i < j;)
      ans = max(ans, nums[i++] + nums[j--]);

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
  public int minPairSum(int[] nums) {
    int ans = 0;

    Arrays.sort(nums);

    for (int i = 0, j = nums.length - 1; i < j;)
      ans = Math.max(ans, nums[i++] + nums[j--]);

    return ans;
  }
}
1
2
3
4
class Solution:
  def minPairSum(self, nums: list[int]) -> int:
    nums.sort()
    return max(nums[i] + nums[len(nums) - 1 - i] for i in range(len(nums) // 2))