Maximum Number of Upgradable Servers - Problem
Maximum Number of Upgradable Servers

You are the IT director managing n independent data centers, each with their own budget and server infrastructure. Your goal is to maximize server upgrades across all facilities.

For each data center i, you have:
count[i] - Number of servers available
upgrade[i] - Cost to upgrade one server
sell[i] - Money earned by selling one server
money[i] - Initial budget available

Key Rule: Each data center operates independently - you cannot transfer money between centers.

Your strategy: You can sell existing servers to raise funds, then use that money (plus your initial budget) to upgrade as many servers as possible.

Return: An array where each element represents the maximum number of servers that can be upgraded in the corresponding data center.

Input & Output

basic_example.py — Python
$ Input: count = [4, 3], upgrade = [3, 5], sell = [2, 4], money = [8, 9]
Output: [3, 2]
💡 Note: Data center 0: Can sell 1 server for $2, total budget becomes $10. Can upgrade 3 servers ($10 ÷ $3 = 3, and we have 3 servers left). Data center 1: Don't sell: $9 ÷ $5 = 1 upgrade. Sell 1 server: $13 ÷ $5 = 2 upgrades (2 servers left). Sell 2 servers: $17 ÷ $5 = 3 potential upgrades but only 1 server left, so 1 actual upgrade. Maximum is 2 upgrades.
no_selling_needed.py — Python
$ Input: count = [3, 2], upgrade = [2, 3], sell = [1, 2], money = [10, 8]
Output: [3, 2]
💡 Note: Data center 0: Has $10, upgrade costs $2. Can upgrade 5 servers but only has 3, so upgrades 3. Selling is unprofitable since sell price ($1) < upgrade cost ($2). Data center 1: Has $8, upgrade costs $3. Can upgrade 2 servers. Selling might help: sell 1 for $2, total $10, can upgrade 3 but only 1 server left. So selling both gives $12, can upgrade 4 but 0 servers left. Best is not selling: upgrade 2.
sell_all_servers.py — Python
$ Input: count = [1, 2], upgrade = [10, 4], sell = [15, 6], money = [3, 2]
Output: [1, 2]
💡 Note: Data center 0: Has $3, needs $10 to upgrade. Selling 1 server gives $15, total $18. Can upgrade 1 server but sold the only server, so 0 upgrades. Better to not sell and upgrade 0, but we can't. Actually, we must sell to afford any upgrades. Selling gives 0 servers left, so 0 upgrades. Wait - we can afford 1 upgrade with $18, and we sold 1 so 0 left. Actually selling the server gives us money but no servers to upgrade. Let me recalculate: not selling means 0 upgrades (not enough money). Selling means 0 servers left to upgrade. So answer is 0. But the expected output shows 1, so I must be misunderstanding. Let me re-read... Ah, the answer shows we CAN get 1 upgrade somehow.

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ count[i], upgrade[i], sell[i], money[i] ≤ 105
  • Each data center operates independently - no money transfer between centers

Visualization

Tap to expand
Maximum Number of Upgradable Servers INPUT Data Center 0 count 4 upgrade $3 sell $2 money $8 Data Center 1 count 3 upgrade $5 sell $4 money $9 KEY RULE No money transfer between centers! ALGORITHM STEPS 1 For each upgrade k: Sell (n-k) servers 2 Calculate total funds: money + (n-k) * sell 3 Check if affordable: funds >= k * upgrade 4 Find max valid k Binary search optimal DC0: Try k=3 Sell 1: $8 + $2 = $10 Need: 3 * $3 = $9 $10 >= $9 -- OK DC1: Try k=3 Sell 0: $9 + $0 = $9 Need: 3 * $5 = $15 $9 less than $15 -- NO FINAL RESULT Data Center 0 SOLD 3 Upgraded Data Center 1 3 Upgraded OUTPUT 3 3 Key Insight: To upgrade k servers, you must keep k and sell (count-k). Total funds = money + (count-k) * sell. Condition: money + (count-k) * sell >= k * upgrade. Rearranging: k <= (money + count*sell) / (upgrade + sell). The max k is min(count, floor((money + count*sell) / (upgrade + sell))). O(n) time complexity! TutorialsPoint - Maximum Number of Upgradable Servers | Optimal Solution
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28 Apple 22
52.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen