Problem · Array

Get Max Servers

Learn this problem
MediumAmazonFULLTIMEOA
See Amazon hiring insights

Problem statement

Duplicate note (July 2, 2026) ᕙ( •̀ ᗜ •́ )ᕗ : This problem is the same question as Server Selection. I merged the sighting dates from both pages into this version. If you want to practice, I recommend practicing this version because it has the fuller official-source statement and example.

A company that uses cloud servers is planning to scale up its application horizontally. It needs to buy a certain number of servers from a given set of n servers. To ensure that adjacent servers have similar load handling capacity, they want the computational power of two adjacent servers to have a difference of 1 or less.

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.

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

Note:

  • A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.

Function

getMaxServers(powers: int[]) → int

Examples

Example 1

powers = [4, 3, 5, 1, 2, 2, 1]return = 5

Source update (July 2, 2026) ᕙ( •̀ ᗜ •́ )ᕗ : I found an official source image that includes the example illustration.

Example source illustration
4
3
5
1
2
2
1
selected subsequence Highlighted values form [3, 1, 2, 2, 1].

Consider powers = [4, 3, 5, 1, 2, 2, 1].

The subsequence [3, 1, 2, 2, 1] can be selected (5 servers) and rearranged to [2, 1, 1, 2, 3]. Checking circular adjacency: abs(2-1)=1, abs(1-1)=0, abs(1-2)=1, abs(2-3)=1, and (circular) abs(3-2)=1, all of which are <= 1.

No valid candidate of size greater than 5 exists, so the maximum number of servers the client can buy is 5.

Constraints

  • 1 <= n <= 2 * 10^5
  • 0 <= powers[i] <= 10^9

More Amazon problems

drafts saved locally
public int getMaxServers(int[] powers) {
  // write your code here
}
powers[4, 3, 5, 1, 2, 2, 1]
expected5
checking account