Super Washing Machines - Problem

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

example_1.py โ€” Basic Balance
$ Input: machines = [1, 0, 5]
โ€บ Output: 3
๐Ÿ’ก Note: Total dresses = 6, so each machine should have 2. Machine 2 has 3 extra dresses and needs to give them all away, which takes 3 moves (since it can only give 1 dress per move). This becomes the bottleneck.
example_2.py โ€” Already Balanced
$ Input: machines = [2, 2, 2]
โ€บ Output: 0
๐Ÿ’ก Note: All machines already have the same number of dresses (2 each), so no moves are needed.
example_3.py โ€” Impossible Case
$ Input: machines = [0, 3, 0]
โ€บ Output: -1
๐Ÿ’ก Note: Total dresses = 3, and we have 3 machines, so each should have 1 dress. However, the middle machine cannot pass dresses to both neighbors simultaneously in the optimal number of moves, but actually this is solvable in 2 moves. Let me recalculate: this should output 2, not -1. Better example: machines = [1, 2] gives -1 since 3 dresses cannot be split evenly between 2 machines.

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

Visualization

Tap to expand
Water Tank Analogy: Balancing LevelsTank 11 dressTank 20 dressesTank 35 dressesFlow pipeFlow pipeTarget level (2 each)3 units must flow out๐ŸŽฏ Key Insight: Tank 3 is the bottleneck - it must release 3 units, taking 3 time steps
Understanding the Visualization
1
Identify Imbalances
Calculate how many dresses each machine needs to give away or receive
2
Find Bottlenecks
Determine which machine or flow path will take the longest
3
Calculate Minimum
The answer is the maximum bottleneck found
Key Takeaway
๐ŸŽฏ Key Insight: The bottleneck determines the minimum time needed. It's either the machine that must give away the most dresses, or the point where cumulative flow imbalance is highest.
Asked in
Google 15 Amazon 8 Microsoft 6 Meta 4
27.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen