Good morning! Here's our prompt for today.
We have an array of length 2 * n (even length) that consists of random integers.
[1, 3, 2, 6, 5, 4]
We are asked to create pairs out of these integers, like such:
[[1, 3], [2, 6], [5, 4]]
How can we divide up the pairs such that the sum of smaller integers in each pair is maximized?

Here's an example input: [3, 4, 2, 5]
. The return value is 6
because the maximum sum of pairs is 6
= min(2, 3) + min(4, 5)
.
Note that negative numbers may also be included.
Constraints
- Length of the array <=
100000
- The values will always contain integer values between
-1000
and1000
- The final answer will always fit in the integer value
- Expected time complexity :
O(nlogn)
- Expected space complexity :
O(1)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
25
var assert = require('assert');
​
function maxOfMinPairs(nums) {
// fill in
return nums;
}
​
console.log(maxOfMinPairs([1, 3, 2, 6, 5, 4]));
​
try {
assert.equal(maxOfMinPairs([3, 4, 2, 5]), 6);
​
console.log('PASSED: ' + 'maxOfMinPairs([3, 4, 2, 5]) should return 6');
} catch (err) {
console.log(err);
}
​
try {
assert.equal(maxOfMinPairs([1, 3, 2, 6, 5, 4]), 9);
​
console.log('PASSED: ' + 'maxOfMinPairs([1, 3, 2, 6, 5, 4]) should return 9');
} catch (err) {
console.log(err);
}
​
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's our guided, illustrated walk-through.
How do I use this guide?