Maximize memory capacity with primary-backup servers
AmazonSoftware Engineer · Online AssessmentLast updated
This question evaluates algorithmic reasoning in constrained pairings and resource allocation, focusing on maximizing aggregate memory under pairwise feasibility constraints while managing large input sizes.
You are helping to optimize the capacity of a cloud system.
There are n servers, where n is always even. The memory capacity of the i-th server is given by an integer array memory of length n.
When there are n = 2 * x servers:
- Exactly
xservers will be chosen as primary servers. - The remaining
xservers will be backup servers.
For every primary server P, there must exist a distinct backup server B such that:
memory[B] >= memory[P]
(Each server can be used as either primary or backup in exactly one such pair, so the n servers are partitioned into x (primary, backup) pairs.)
The system memory capacity is defined as the sum of the memory capacities of all primary servers:
capacity = sum(memory[P] over all primary servers P)
Your task is to determine the maximum possible system memory capacity that can be obtained by:
- Partitioning the
nservers inton/2disjoint (primary, backup) pairs, and - Respecting the constraint
memory[backup] >= memory[primary]within each pair.
Input
- An even integer
n(number of servers). - An integer array
memoryof lengthn, wherememory[i]is the memory capacity of the i-th server.
You may assume:
2 <= n <= 2 * 10^5, andnis even.1 <= memory[i] <= 10^9.
Output
- Return a single integer: the maximum possible system memory capacity.
Complexity requirement
Design an algorithm efficient for n up to about 2 * 10^5 (i.e., significantly better than O(n^2) time).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.