Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Max Of Min Pairs (Main Thread)

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.

Constraints

  • Length of the array <= 100000
  • The values will always contain integer values between -1000 and 1000
  • The final answer will always fit in the integer value
  • Expected time complexity : O(nlogn)
  • Expected space complexity : O(1)

You can see the full challenge with visuals at this link.

Challenges • Asked almost 5 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Jul 11, 2019:

This is the main discussion thread generated for Max Of Min Pairs.

Jake from AlgoDaily Commented on Jul 11, 2019:

Thanks to user Matt for the initial test cases!

Alejandro Matos Commented on Jul 23, 2019:

this is so simple I thought they forgot to give the solution...

Alain Cohen Commented on Jul 24, 2019:

If you need to sort the array, the complexity is at least O(nlog(n))

Anonymous Commented on Jul 26, 2019:

Please fix the time complexity comment. Sorting is O(n*log(n)), not O(n).

Anonymous Commented on Sep 06, 2019:

The max sum of pairs of [3, 4, 2, 5] isn't 6, it's 9.
[2,4] and [3, 5] = 4 + 5 = 9.

Anonymous Commented on Oct 20, 2019:

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

Anonymous Commented on Oct 20, 2019:

Ah, I see where my error is. It's the even indices, not even numbers.

Jake from AlgoDaily Commented on Nov 28, 2019:

You're looking for the minimum of each pair, so:

6 = min(2, 3) + min(4, 5)