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
22
def max_of_min_pairs(nums):
# fill in
return max_sum_of_pairs
​
​
import unittest
​
​
class Test(unittest.TestCase):
def test_1(self):
assert max_of_min_pairs([3, 4, 2, 5]) == 6
print("PASSED: max_of_min_pairs([3, 4, 2, 5]) should return 6")
​
def test_2(self):
assert max_of_min_pairs([1, 3, 2, 6, 5, 4]) == 9
print("PASSED: max_of_min_pairs([1, 3, 2, 6, 5, 4]) should return 9")
​
​
if __name__ == "__main__":
unittest.main(verbosity=2)
print("Nice job, 2/2 tests passed!")
​
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's our guided, illustrated walk-through.
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.