#3776

Minimum Moves to Balance Circular Array

medium · 40.2% accepted · 111 likes · top 20%

array · greedy · sorting

Description

You are given a circular array balance of length n, where balance[i] is the net balance of person i.

In one move, a person can transfer exactly 1 unit of balance to either their left or right neighbor.

Return the minimum number of moves required so that every person has a non-negative balance. If it is impossible, return -1.

Note: You are guaranteed that at most 1 index has a negative balance initially.

Solution