Make the array elements equal by performing given operations minimum number of times | Function to calculate the minimum operations of given type required to make the array elements equal ; Stores the total count of operations ; Traverse the array ; Update difference between pairs of adjacent elements ; Store the maximum count of operations ; Rest of the elements ; Total Operation - Maximum Operation ; Driver Code ; Given array ; Size of the array
def minOperation ( a , N ) : NEW_LINE INDENT totOps = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT totOps += abs ( a [ i ] - a [ i + 1 ] ) NEW_LINE DEDENT maxOps = max ( abs ( a [ 0 ] - a [ 1 ] ) , abs ( a [ N - 1 ] - a [ N - 2 ] ) ) NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT maxOps = max ( maxOps , abs ( a [ i ] - a [ i - 1 ] ) + abs ( a [ i ] - a [ i + 1 ] ) - abs ( a [ i - 1 ] - a [ i + 1 ] ) ) NEW_LINE DEDENT print ( totOps - maxOps ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , - 1 , 0 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE minOperation ( arr , N ) NEW_LINE DEDENT