I stared at this longer than I should have.
Sort the array and use a greedy two-pointer approach: pair the largest possible backup with the largest possible primary that it can cover, ensuring each primary is paired with a backup of at least its size. To maximize the sum of primaries, aim to make the larger half of the sorted array the backups and the smaller half the primaries, then adjust pairings to satisfy the condition. This yields an optimal solution in O(n log n) time.
Pro tip: Clarify that the goal is to maximize the sum of primaries, not the number of pairs or the sum of backups. Emphasize that sorting and greedy pairing is optimal because any valid pairing can be transformed into a sorted greedy pairing without reducing the primary sum.
Restate the problem: given an array of memory values, pair servers such that in each pair, the backup's memory >= primary's memory. Maximize the sum of primary memories. Clarify that each server can be used at most once, and pairs are disjoint.
Sort the memory values in non-decreasing order. This allows efficient pairing by considering smaller elements as potential primaries and larger elements as potential backups.
Initialize two pointers: one at the start (smallest) and one at the middle (or use a greedy approach). Try to pair the largest possible primary with the smallest possible backup that is >= it. Alternatively, split the sorted array into two halves: the first half as primaries and the second half as backups, then adjust pairings to ensure each backup >= its primary.
To maximize the sum, we want the primaries to be as large as possible. However, each primary must have a backup >= it. So we should try to make the larger elements primaries if they can be paired with even larger backups. The optimal strategy is to pair the largest element that can be a primary with the smallest possible backup that is >= it, and repeat.
Test with small arrays, duplicates, and cases where no valid pairing exists. Ensure the algorithm handles odd lengths (one server left unpaired) and returns the maximum sum.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.