My first instinct was right but I couldn't articulate why.
Sort the memory values, then use a greedy two-pointer strategy to pair the largest possible backups with the smallest possible primaries, ensuring each primary gets a backup at least as large. To maximize the sum of primaries, we want the largest n/2 values to be primaries, but we must verify that each can be paired with a backup from the smaller half. The optimal solution is to take the largest n/2 values as primaries and pair them with the smallest n/2 values as backups in sorted order.
Pro tip: Emphasize that the greedy choice is safe because any feasible solution can be transformed into the greedy one without decreasing the primary sum, and mention that this is a classic exchange argument. Also, note that the problem is equivalent to maximizing the sum of the larger half of a partition where each larger element is paired with a smaller or equal element.
Restate the problem: given an even number of servers with memory values, split into two equal-sized groups (primary and backup) such that each primary is paired with a backup of >= memory. Maximize the sum of primary memories.
Sort the array of memory values in non-decreasing order. This allows us to reason about which elements can serve as backups for which primaries.
The optimal primary set is the largest n/2 elements. Pair the smallest primary with the smallest backup, the next smallest primary with the next smallest backup, and so on. This ensures each primary has a backup at least as large.
Show that any feasible solution can be transformed into the greedy solution without decreasing the primary sum. If a smaller element is a primary while a larger element is a backup, swapping them maintains feasibility and does not decrease the sum.
Sorting takes O(n log n) time and O(1) extra space (if in-place). The pairing is O(n). Discuss edge cases like all equal values or when the largest n/2 cannot be paired (but they always can because the smallest n/2 are <= the largest n/2).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.