Find Minimum Cost to Shift Machines
Updated
Problem statement
There are n regions that host servers. Region i has machineCount[i] machines. The team wants exactly 3 regions whose machine counts are finalMachineCount[0], finalMachineCount[1] and finalMachineCount[2].
You can use two operations:
- Add or remove a machine in any existing region. The region must have at least one machine before and after. This costs
1. - Move all machines from one region into another existing region. The emptied region is destroyed. This costs
shiftingCost.
Return the minimum total cost so that some 3 regions hold exactly the required counts. Other regions may be left over at the end; they cost nothing.
Function
findMinimumCostToShiftMachines(machineCount: int[], finalMachineCount: int[], shiftingCost: int) → intExamples
Example 1
machineCount = [2, 4, 5, 3]finalMachineCount = [4, 4, 4]shiftingCost = 5return = 2
On increasing the number of machines of the 4th region by 1 and decreasing the number of machines of the 3rd region by 1, the new machineCount = [2, 4, 4, 4]. Total cost for these operations = 1+1=2.
Use the 2nd, 3rd, and 4th regions as the required servers, leaving behind the 1st region.
Example 2
machineCount = [2, 3, 5, 7]finalMachineCount = [5, 10, 5]shiftingCost = 2return = 5Merge the region with 3 machines into the region with 7 machines for shiftingCost = 2; that region now holds 10. The regions hold 2, 10 and 5 machines. Add 3 machines one at a time to the region with 2, for a cost of 3, so it holds 5. The three regions now hold 5, 10 and 5 machines, so the total cost is 2 + 3 = 5. No cheaper sequence exists.
Constraints
3 ≤ n ≤ 10, wheren = machineCount.lengthfinalMachineCount.length == 31 ≤ machineCount[i], finalMachineCount[i] ≤ 10^61 ≤ shiftingCost ≤ 10^6
More Cisco interview questions
- Check if a String Is a PalindromePHONE SCREEN · Seen Nov 2026
- Find Unique ElementsPHONE SCREEN · Seen Nov 2026
- First Letter to Appear TwicePHONE SCREEN · Seen Nov 2026
- Middle of a Linked ListPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Reverse a StringPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- K-Step Bit Flip with RetentionPHONE SCREEN · Seen Nov 2026
- Todo Task Manager OperationsONSITE INTERVIEW · Seen Oct 2026
- Find the Duplicate Number Without Modifying the ArrayONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Split Array Largest SumAuxoAI · OA
- Get Minimum IncrementIBM · OAIBM interview questions
- Minimum Number of Taps to Water a GardenGoldman Sachs · OAGoldman Sachs interview questions
- Maximum Income from Asset TradingJefferies · OA
- Best Time to Buy and Sell StockFlexTrade · OAFlexTrade interview questions
- Maximum Even-Sum Neighboring PairsDRW · OADRW interview questions
- Minimize Total Input Cost (for LTMS)Salesforce · OASalesforce interview questions
- Maximize Protected City PopulationAmazon · OA
- Network FormationPalantir · OAPalantir interview questions