AlgoDaily 15: Max of Min Pairs

https://algodaily.com/challenges/max-of-min-pairs

This requires a bit of thought. We want to get the highest possible smaller number in each pair.

The first thought is that we need to sort the numbers before we do anything else, so that we can allocate pairs with the largest possible minimum.

To get the largest possible minimum in the pair, you have to “waste” a larger number to accompany it. This should be the smallest possible number that is larger than the minimum in the pair.

It seems that after sorting the whole list, the numbers will naturally be in the order for the pairs to achieve the above. I have a feeling that some list exists where this won’t hold true, but I can’t think of an example so perhaps not.

So we sort the numbers, then sum up the numbers at every even position.

function maxOfMinPairs(numbers) {
	let sum = 0;
	numbers = [...numbers].sort((a, b) => a - b);
	for (i = 0; i < numbers.length; i += 2) {
		sum += numbers[i];
	}
	return sum;
}

View post: AlgoDaily 15: Max of Min Pairs