# Minimum Sum

Hey guys, I hope you guys had a great Saturday! For today’s algorithm problem, I have to write a function that does the following: Given an array of **2n** integers, your task is to group these integers into **n** pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. Here’s an example:

**Input:** [1,4,3,2]

**Output:** 4

**Explanation:** n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

I started off by sorting the input array and declaring a variable which would pretty much hold the minimum sum of the array. I originally started out by having a for loop iterate for half the length of the array, but I realized it would be an issue because as the inputs in the array increase, the later inputs of the array wouldn’t even be run. So I just run it for the whole length of the array and increased the iterator by two each time. Here’s what I did within each iteration:

`var arrayPairSum = function(nums) {`

nums.sort((a, b) => a - b);

minSum = 0;

for (let i = 0; i < nums.length; i += 2) {

minSum += Math.min(nums[i], nums[i + 1]);

}

return minSum;

};

It ended up being pretty simple and straightforward. Anyways, if you guys learned something interesting or think the solution is pretty cool, click the clap button! If you have any better ways to solve it, feel free to leave a reply or contact me, thanks!