FastPrepFastPrep
Problem Brief

Warehouse Allocation (Distribute)

INTERNOA

In a bustling warehouse of Amazon, a dedicated caretaker oversees a collection of n piles of boxes, each containing a different number of goods waiting to be shipped. To ensure that the treasures are evenly distributed, the caretaker is allowed to perform a magical operation: they can choose any two distinct piles, i and j, as long as pile i has at least one box. With a gentle flick of their wrist, they can move a box from pile i to pile j, thereby balancing the wealth between the two. The caretaker's quest is to minimize the difference between the pile with the most boxes and the one with the fewest. This difference is known as d, and the caretaker seeks to find the least number of operations required to achieve this fair distribution of boxes. Your task is to complete the function findMinimumOperations, which will reveal the minimum operations needed to attain this harmony.

❀⊱ Credit to eva 🌷 ⊰❀

1Example 1

Input
boxes = [5, 5, 8, 7]
Output
2
Explanation
Example 1 illustration
Consider the number of piles to be n = 4 and the boxes in them are boxes = [5, 5, 8, 7]. The minimum possible difference that can be achieved is 1 by transforming the piles into [6, 6, 7, 6] as below. Hence the answer is 2.

2Example 2

Input
boxes = [2, 4, 1]
Output
1
Explanation
Move a box from pile 2 to pile 3: [2, 4, 1] -> [2, 3, 2]

3Example 3

Input
boxes = [4, 4, 4, 4, 4]
Output
0

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= n <= 10^5
  • 1 <= boxes[i] <= 10^9
  • public long findMinimumOperations(int[] boxes) {
      // write your code here
    }
    
    Input

    boxes

    [5, 5, 8, 7]

    Output

    2

    Sign in to submit your solution.