Server Selection
Learn this problemProblem statement
You are given an array server, where server[i] is the request-handling capacity of one server. Every capacity is a power of 2. You are also given a target load expected_load.
Return the minimum number of servers whose capacities sum to exactly expected_load. Return -1 if no such selection exists.
For this exercise, assume the following selection rules:
- Each array element represents a separate server that may be selected at most once. Servers with equal capacities remain separate choices.
- A selected server contributes its entire capacity; capacities cannot be split or partially used. A total greater than
expected_loadis not valid. - If
expected_loadis0, the empty selection is valid and the answer is0.
The callable minimumServers(server, expected_load) returns only the minimum count, not the selected indices. The target uses a long integer in Java and a long long integer in C++.
Function
minimumServers(server: int[], expected_load: long) → intExamples
Example 1
server = [1,2,8,4,2]expected_load = 13return = 3Select capacities 8, 4, and 1, totaling 13. The two largest servers total only 12, so fewer than three cannot work. This is an authored practice example.
Example 2
server = [2,2,8]expected_load = 7return = -1Every possible total is even, so no selection totals 7. The server with capacity 8 cannot be used partially. This is an authored practice example.
Example 3
server = [16,8,4]expected_load = 0return = 0Select no servers to meet the zero load. This is an authored practice example.
Constraints
1 <= server.length <= 100000.- For this exercise, assume
server[i] = 2^bfor an integerbwith0 <= b <= 30. - For this exercise, assume
0 <= expected_load <= 10^15.