Imagine you have n super washing machines arranged in a straight line in your laundromat. Each machine currently contains a different number of dresses (or might be empty). Your goal is to redistribute the dresses so that every machine has exactly the same number of dresses.
Here's how the redistribution works: In each move, you can select any number of machines (from 1 to n) and simultaneously pass one dress from each selected machine to one of its adjacent neighbors (left or right).
Your mission: Find the minimum number of moves required to achieve perfect balance across all machines. If it's impossible to balance the dresses equally, return -1.
Example: If you have machines with [1, 0, 5] dresses, the total is 6 dresses across 3 machines, so each should have 2 dresses. You need to move dresses from machine 2 (with 5) to machines 0 and 1.
Input & Output
Constraints
- 1 โค machines.length โค 104
- 0 โค machines[i] โค 105
- The sum of all dress counts fits in a 32-bit integer
- Important: Each move allows multiple machines to pass dresses simultaneously