Get Max Servers
Updated
Problem statement
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 single server is a valid circle by itself: it is adjacent only to itself, and the difference is 0.
- 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[]) → intExamples
Example 1
powers = [4, 3, 5, 1, 2, 2, 1]return = 5Consider powers = [4, 3, 5, 1, 2, 2, 1].
The servers with powers 3, 1, 2, 2, 1 can be bought (5 servers) and arranged in the circle [2, 1, 1, 2, 3]. Checking neighbours: abs(2-1)=1, abs(1-1)=0, abs(1-2)=1, abs(2-3)=1, and, from the last server back to the first, abs(3-2)=1. All of them are at most 1.
No circle of 6 servers exists. Any 6 servers still include a 1 together with a 4 or a 5. A circle that holds a 1 and a 4 (or a 5) must climb from 1 up to 4 (or 5) and come back, so it passes the power 3 twice, but there is only one 3. So the maximum number of servers the client can buy is 5.
Constraints
1 <= n <= 2 * 10^50 <= powers[i] <= 10^9
More Amazon interview questions
- Get Smallest Base SegmentOA · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- 3SumPHONE SCREEN · Seen Nov 2026
- Circular Primes Below a LimitPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Distinct Prefix CostGoldman Sachs · OAGoldman Sachs interview questions
- Maximize Minimum Deployment DifficultyJP Morgan Chase · OAJP Morgan Chase interview questions
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions
- EV Charging Cost OptimizationTata Consultancy Services · OATata Consultancy Services interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Minimum Removals to Balance ArraySalesforce · OASalesforce interview questions
Amazon interview experiences
- Amazon SWE OA with AI Coding: 1 Experience
- Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
- Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
- Amazon BIE Intern SQL and Interview Questions: 17 Experiences
- Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
- Amazon SDE II (L5) Interview Questions: 5 Experiences