FastPrepFastPrep
Problem Brief

Get Max Servers

FULLTIMEOA
See Amazon online assessment and hiring insights

SDE II

Another purchaing servers problem for AMZ full-time - Purchase Servers

Following is the official version statement I found on 06-25-2025..The first paragraph is imcomplete, so I did not include it this time..

Given the computational power of all the n servers as an array of integers powers, find the maximum number of servers that the client can buy such that the selected set of servers can be rearranged in a way that the absolute difference between the computational power of two adjacent servers is less than or equal to 1. The client wants to create a circular network so the first and last servers in the sequence are also considered adjacent.

More formally, a sequence candidate[] of length m is classified as a candidate for selection by the client if it can be rearranged in a way such that abs(candidate[i] - candidate[i+1]) <= 1 for 0 <= i < m - 1 and abs (candidate[m-1] - candidate[0]) <= 1. (Yus, I double checked it is called "candidate".. no typo here :)

Find the maximum number of servers the client can buy from the n avalable servers.

Below is the problem statement we had before --

Given a list of n servers, with the computing power of ith server at powers[i]. A client wants to buy some K servers out of these n servers. The condition is that when these K servers are rearranged, the absolute difference between the computing powers of two adjacent servers should be less then or equal to 1. Also, these servers will form a circular network. So the first and last servers will also be considered adjacent. Find the maximum number of servers K, which the client can buy.

1Example 1

Input
powers = [4, 3, 5, 1, 2, 2, 1]
Output
5
Explanation
Example 1 illustration
Client can buy 5 servers -> {3, 1, 2, 2, 1} and rearrange them to {2, 1, 1, 2, 3}. Complete explanation was found and added on 06-25-2025:

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= n <= 2 * 10^5
  • 0 <= powers[i] <= 10^9
  • Complete problem constraints was added on 06-25-2025
  • public int findMaximumNumberOfServers(int[] powers) {
      // write your code here
    }
    
    Input

    powers

    [4, 3, 5, 1, 2, 2, 1]

    Output

    5

    Sign in to submit your solution.