Minimum Circular Redistribution Cost
Learn this problemProblem statement
A warehouse has n identical containers arranged in a circle. Adjacent containers are one unit apart, and the goal is to make every container hold the same number of products.
- Choose either clockwise or counterclockwise movement; all transfers must use that same direction.
- Products may be collected from containers with excess units and delivered to containers with deficits.
- Moving one product across one edge costs
1.
Return the minimum total transfer cost. It is guaranteed that equal redistribution is possible.
Function
findMinimumCost(products: int[]) → longExamples
Example 1
products = [3, 4, 6, 6, 6]return = 7Consider a circular arrangement of containers. The units in each container are products = [3, 4, 6, 6, 6].
Option 1:
Start at the 3rd position and move clockwise. Collect one product each from the 3rd, 4th, and 5th positions.
Transfer the products from:
- the 5th position to the 1st position, the cost is
1. - the 4th position to the 1st position, the cost is
2. - the 3rd position to the 2nd position, the cost is
4.
Now each container has 5 units and the total cost is 1 + 2 + 4 = 7.
Option 2:
Start at the 5th position moving anti-clockwise. Collect one product each from the 5th, 4th, and 3rd positions.
Transfer the product from:
- the 3rd position to the 1st position, the cost is
2. - the 4th position to the 1st position, the cost is
3. - the 5th position to the 2nd position, the cost is
3.
Now each container has 5 units and the total cost is 2 + 3 + 3 = 8. Return 7, the minimum cost achievable.
Example 2
products = [1, 11, 1, 1, 1]return = 20The final average is 3. One container has 8 extra products. Moving in either fixed direction, send 2 products to each of the other four containers, for total cost 2*1 + 2*2 + 2*3 + 2*4 = 20.
Constraints
1 <= products.length <= 2 * 10^50 <= products[i] <= 10^9- The answer may exceed the 32-bit integer range.
More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026