Find the maximum of the minimum in pairs of array - Coding Problem

Find the maximum of all the minimums in pairs of array.

Initially, the array given is a single array. You should break the array into smaller pairs of array such that the sum of smaller numbers in array is maximum in all possible combinations.

Solution

Let’s consider the array given is [23, 34, 55, 26, 6, 19].
The main criteria for this question is that the count of numbers in array must be even, so that they can be arranged in arrays of pairs.
To break the entire range into multiple pairs in such a way that each pair has a minimum which will only give us the maximum possible value when added with all minimums.
Basically, if we sort the numbers in ascending order and then take alternate numbers for sum, it will be fulfilling both requirements.
Like, the sorted array would be like [6, 19, 23, 26, 34, 55]. Once divided them in smaller arrays would be [6, 19] , [23, 26], [34, 55]. The highlighted numbers are the minimum among the pairs. When added together, they will give the maximum of minimums.

The same logic works with negative numbers:

Implementation

Javascript
Python
function maximumOfMinimums(numbers) {
	var sortedArray = numbers.sort(function(a, b) {
		return a - b;
	});
	var totalofMinimums = sortedArray[0];
	for (var i = 1; i < sortedArray.length; i++) {
		if (i % 2 == 0) {
			totalofMinimums += sortedArray[i];
		}
	}
	return totalofMinimums;
};
//Input: maximumOfMinimums([23, 34, 55, 26, 6, 19])
//Output: 63

//Input: maximumOfMinimums([11, -3, 20, 32, 45, 6])
//Output: 40
def maximumOfMinimums(numbers):
  numbers.sort()
  totalofMinimums = 0
  for i in range(len(numbers)):
    if i % 2 == 0:
     totalofMinimums += numbers[i];

  return totalofMinimums

# Input: print(maximumOfMinimums([23, 34, 55, 26, 6, 19]));
# Output: 63
// third
fourth