Minimum Circular Redistribution Cost
Problem 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.
Examples
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.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 1 more worked example, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.