Here is the interview question prompt, presented for reference.
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.
100000
-1000
and 1000
O(nlogn)
O(1)
You can see the full challenge with visuals at this link.
Challenges • Asked over 5 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Max Of Min Pairs.
Thanks to user Matt for the initial test cases!
this is so simple I thought they forgot to give the solution...
If you need to sort the array, the complexity is at least O(nlog(n))
Please fix the time complexity comment. Sorting is O(n*log(n)), not O(n).
The max sum of pairs of [3, 4, 2, 5] isn't 6, it's 9.
[2,4] and [3, 5] = 4 + 5 = 9.
You wouldn't take the even number, since if the the numbers are sorted in ascending order, the odd number would be the one that is smaller (it's taking the maximum of the minimums of pairs).
[1, 3, 2, 6, 5, 4] -> [1, 2, 3, 4, 5, 6] -> 1 + 3 + 5 = 9
Ah, I see where my error is. It's the even indices, not even numbers.
You're looking for the minimum of each pair, so:
6 = min(2, 3) + min(4, 5)