Problem · Greedy

Server Selection

Learn this problem
HardAmazonFULLTIMEOA
See Amazon hiring insights

Problem statement

Duplicate note (July 2, 2026) ( ദ്ദി ˙ᗜ˙ ) : This problem is the same question as Get Max Servers. I merged this page's sighting dates into that version, so this page will not be updated anymore. If you want to practice, I recommend practicing Get Max Servers.

Amazon Web Services (AWS) provides highly scalable solutions for applications hosted on their servers. A company using AWS is planning to scale up horizontally and wants to buy servers from a list of available options.

Find the maximum number of servers (as a subsequence from the list) that can be rearranged so that the absolute difference between adjacent servers (including circular adjacency) is ≤ 1.

Conditions:

  • A circular sequence is formed, so the first and last servers are also considered adjacent.
  • A subsequence means elements can be removed, but you are free to rearrange the chosen elements.

Formal:

Given an array powers[] of n integers, find the maximum subsequence length m such that the chosen elements can be rearranged into a circular array a where:

  • abs(a[i] - a[i+1]) ≤ 1 for all valid i, and
  • abs(a[m-1] - a[0]) ≤ 1 where m is the length of the subsequence.

Function

maxServers(powers: int[]) → int

Examples

Example 1

powers = [4, 3, 5, 1, 2, 1]return = 4
Valid candidate subsequences: [1, 2, 2, 1] is already a valid circular arrangement; [3, 1, 2, 2] can be rearranged to [1, 2, 3, 2], which is valid. An example of an invalid selection is [3, 1, 2], since no rearrangement makes all circular adjacent differences <= 1. The maximum valid length is 4.

Constraints

  • powers contains n integers.
  • The answer is the maximum number of elements that can be selected and rearranged into a valid circular arrangement.
  • In the resulting circular arrangement, abs(a[i] - a[i+1]) ≤ 1 for all consecutive elements, and abs(a[m-1] - a[0]) ≤ 1, where m is the length of the chosen arrangement.

More Amazon problems

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