The developers are working on optimising the capacity of their cloud system. In the system, there are n servers where the memory capacity of the i-th server is represented by the array memory[i].
A system always contains an even number of servers.
If the system has 2x servers, then x of them will be primary and the other x will be backup servers.
For each primary server P, there exists a backup server B where the memory capacity of B ≥ memory capacity of P. The system's memory capacity is the sum of the memory capacity of all the primary servers.
Given n servers and an array memory, find the maximum system memory capacity that can be formed using the n servers.
Example
Given 5 servers as [serverA, serverB, serverC, serverD, serverE] having memory = [2, 4, 3, 1, 2].
In the second configuration, the system memory capacity is memory[serverA] + memory[serverD] = 3.
While in the third configuration, it is memory[serverA] + memory[serverC] = 5.
Hence, the maximum system memory capacity is 5.
Function Description
Complete the function maximumCapacity in the editor below.
maximumCapacity has the following parameter:
• int memory[n]: the memory capacity of the given servers
Returns
• long int: the maximum system memory capacity
Constraints
2 ≤ n ≤ 2*10^5
1 ≤ size[i] ≤ 10^9
Sample Case 0:
Input:
4
1 2 1 2
Output:
3
Explanation:
Here, we have 4 servers [serverA, serverB, serverC, serverD] having memory sizes as [1, 2, 1, 2].
We can choose serverA and serverB as primary servers, and serverC and serverD as their respective backup.
The conditions hold true since memory[serverC] ≥ memory[serverA] and memory[serverD] ≥ memory[serverB].
Hence, the maximum system memory capacity is 3.
Sample Case 1:
Input:
3
1 2 1
Output:
1
Explanation:
Here, we have 3 servers [serverA, serverB, serverC] having memory sizes as [1, 2, 1].
We can choose serverA as a primary server, and serverB as its respective backup server.
The conditions hold true since memory[serverB] ≥ memory[serverA].
Hence, the maximum system memory capacity is 1.
A system always contains an even number of servers.
If the system has 2x servers, then x of them will be primary and the other x will be backup servers.
For each primary server P, there exists a backup server B where the memory capacity of B ≥ memory capacity of P. The system's memory capacity is the sum of the memory capacity of all the primary servers.
Given n servers and an array memory, find the maximum system memory capacity that can be formed using the n servers.
Example
Given 5 servers as [serverA, serverB, serverC, serverD, serverE] having memory = [2, 4, 3, 1, 2].
Primary Servers Options |
Backup Servers Options |
Conditions | Valid Option |
---|---|---|---|
serverA, serverB |
serverC, serverD |
memory[serverA] ≤ memory[serverC], memory[serverB] ≤ memory[serverD] |
No |
serverA, serverD |
serverB, serverE |
memory[serverA] ≤ memory[serverB], memory[serverD] ≤ memory[serverE] |
Yes |
serverA, serverC |
serverE, serverB |
memory[serverA] ≤ memory[serverE], memory[serverC] ≤ memory[serverB] |
Yes |
In the second configuration, the system memory capacity is memory[serverA] + memory[serverD] = 3.
While in the third configuration, it is memory[serverA] + memory[serverC] = 5.
Hence, the maximum system memory capacity is 5.
Function Description
Complete the function maximumCapacity in the editor below.
maximumCapacity has the following parameter:
• int memory[n]: the memory capacity of the given servers
Returns
• long int: the maximum system memory capacity
Constraints
2 ≤ n ≤ 2*10^5
1 ≤ size[i] ≤ 10^9
Sample Case 0:
Input:
4
1 2 1 2
Output:
3
Explanation:
Here, we have 4 servers [serverA, serverB, serverC, serverD] having memory sizes as [1, 2, 1, 2].
We can choose serverA and serverB as primary servers, and serverC and serverD as their respective backup.
The conditions hold true since memory[serverC] ≥ memory[serverA] and memory[serverD] ≥ memory[serverB].
Hence, the maximum system memory capacity is 3.
Sample Case 1:
Input:
3
1 2 1
Output:
1
Explanation:
Here, we have 3 servers [serverA, serverB, serverC] having memory sizes as [1, 2, 1].
We can choose serverA as a primary server, and serverB as its respective backup server.
The conditions hold true since memory[serverB] ≥ memory[serverA].
Hence, the maximum system memory capacity is 1.
Get one-to-one training from Google Facebook engineers
Top-notch Professionals
Learn from Facebook and Google senior engineers interviewed 100+ candidates.
Most recent interview questions and system design topics gathered from aonecode alumnus.
One-to-one online classes. Get feedbacks from real interviewers.
Customized Private Class
Already a coding expert? - Advance straight to hard interview topics of your interest.
New to the ground? - Develop basic coding skills with your own designated mentor.
Days before interview? - Focus on most important problems in target company question bank.