Problem · Array

Server Selection

Learn this problem
MediumPoint72 logoPoint72INTERNNEW GRADOA

Problem 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_load is not valid.
  • If expected_load is 0, the empty selection is valid and the answer is 0.

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) → int

Examples

Example 1

server = [1,2,8,4,2]expected_load = 13return = 3

Select 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 = -1

Every 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 = 0

Select 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^b for an integer b with 0 <= b <= 30.
  • For this exercise, assume 0 <= expected_load <= 10^15.

More Point72 problems

drafts saved locally
public int minimumServers(int[] server, long expected_load) {
    // Write your code here.
}
server[1,2,8,4,2]
expected_load13
expected3
checking account