Balance Dresses Across Adjacent Washing Machines

Quick Overview

Balance Dresses Across Adjacent Washing Machines. Machines stand in a line with initial dress counts machines.

Balance Dresses Across Adjacent Washing Machines

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Machines stand in a line with initial dress counts machines. In one move, any chosen machines may each send one dress to one adjacent machine simultaneously; each sender sends at most one dress in that move. Return the fewest moves needed for all counts to agree, or -1 if balancing is impossible. ```cpp class Solution { public: int findMinMoves(vector<int>& machines); }; ``` ### Examples **Example 1:** ```text machines = [4,0,2] Output: 2 Explanation: [4,0,2] -> [3,1,2] -> [2,2,2]. ``` **Example 2:** ```text machines = [0,4,0] Output: -1 Explanation: Four dresses cannot be split equally among three machines. ``` ### Constraints - There are n=machines.length machines, with n in 1..10000. Every initial count is in 0..100000. ### Clarifying Questions Account for simultaneous transfers, one outgoing dress per chosen machine, cut imbalances and impossible totals. ```hint Read the boundary condition How many dresses must cross the boundary after each prefix of machines? ``` ### Contract considerations Account for simultaneous transfers, one outgoing dress per chosen machine, cut imbalances and impossible totals.

Overview: Balance Dresses Across Adjacent Washing Machines. Machines stand in a line with initial dress counts machines.

Amazon logo
Amazon
Sep 15, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Machines stand in a line with initial dress counts machines. In one move, any chosen machines may each send one dress to one adjacent machine simultaneously; each sender sends at most one dress in that move. Return the fewest moves needed for all counts to agree, or -1 if balancing is impossible.

class Solution {
public:
    int findMinMoves(vector<int>& machines);
};

Examples

Example 1:

machines = [4,0,2]
Output: 2
Explanation: [4,0,2] -> [3,1,2] -> [2,2,2].

Example 2:

machines = [0,4,0]
Output: -1
Explanation: Four dresses cannot be split equally among three machines.

Constraints

  • There are n=machines.length machines, with n in 1..10000. Every initial count is in 0..100000.

Clarifying Questions Guidance

Account for simultaneous transfers, one outgoing dress per chosen machine, cut imbalances and impossible totals.

Contract considerations

Account for simultaneous transfers, one outgoing dress per chosen machine, cut imbalances and impossible totals.