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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 2
    integer, parameter :: nums(2*n) = [1, 4, 3, 2, 6, 2, 6, 5, 1, 2]
    integer :: i, j, k
    integer :: min_val
    integer :: max_sum

    ! Initialize variables
    max_sum = 0

    ! Loop over all possible pairs
    do i = 1, n
        do j = i+1, n
            min_val = min(nums(i), nums(j))
            max_sum = max(max_sum, min_val)
        end do
    end do

    print '(I0)', max_sum

end program main
Compiled
Executed
Correct
module pair_sum

implicit none

contains

function pair_sum_max(nums) result(max_sum)

! 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

integer, intent(in) :: nums(:)
integer :: max_sum, i, j, min_val

! Initialize the maximum sum to 0
max_sum = 0

! Loop through the array in pairs
do i = 1, size(nums), 2
    ! Calculate the minimum value of the two elements in the pair
    min_val = min(nums(i), nums(i+1))
    ! Add the minimum value to the maximum sum
    max_sum = max_sum + min_val
end do

end function pair_sum_max

end module pair_sum

program test_pair_sum

use pair_sum, only : pair_sum_max
implicit none

! Test case 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.
call assert(pair_sum_max([1,4,3,2]) == 4)

! Test case 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.
call assert(pair_sum_max([6,2,6,5,1,2]) == 9)

contains

subroutine assert(condition)

logical, intent(in) :: condition

if (.not. condition) then
    write (*,*) "Assertion failed"
    stop 1
end if

end subroutine assert

end program test_pair_sum
🌐 Data from online sources
def array_pair_sum(nums):
    nums.sort()
    return sum(nums[::2])
  1. First, sort the given 'nums' array/vector in ascending order.
  2. Iterate over the sorted array, with a step of 2, starting from index 0.
  3. During the iteration, accumulate the sum of every ith element (from sorted array) to maximize the sum of minimum elements in pairs.
  4. Return the accumulated sum.
🌐 Data from online sources
#include <vector>
#include <algorithm>

int arrayPairSum(std::vector<int>& nums) {
    std::sort(nums.begin(), nums.end());
    int sum = 0;
    for (int i = 0; i < nums.size(); i += 2) {
        sum += nums[i];
    }
    return sum;
}
  1. First, sort the given 'nums' array/vector in ascending order.
  2. Iterate over the sorted array, with a step of 2, starting from index 0.
  3. During the iteration, accumulate the sum of every ith element (from sorted array) to maximize the sum of minimum elements in pairs.
  4. Return the accumulated sum.