Minimize subtraction followed by increments of adjacent elements required to make all array elements equal | Function to find the minimum number of moves required to make all array elements equal ; Store the total sum of the array ; Calculate total sum of the array ; If the sum is not divisible by N , then print " - 1" ; Stores the average ; Stores the count of operations ; Traverse the array arr [ ] ; Update number of moves required to make current element equal to avg ; Update the overall count ; Return the minimum operations required ; Driver Code
def findMinMoves ( arr , N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if ( sum % N != 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT avg = sum // N NEW_LINE total = 0 NEW_LINE needCount = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT needCount += ( arr [ i ] - avg ) NEW_LINE total = max ( max ( abs ( needCount ) , arr [ i ] - avg ) , total ) NEW_LINE DEDENT return total NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMinMoves ( arr , N ) ) NEW_LINE DEDENT