Minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1 | Function to find the minimum value of the sum of absolute difference between all pairs of arrays ; Stores the sum of array elements ; Find the sum of array element ; Store the value of sum % N ; Return the resultant value ; Driver Code
def minSumDifference ( ar , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += ar [ i ] NEW_LINE DEDENT rem = sum % n NEW_LINE return rem * ( n - rem ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 8 , 5 , 2 , 1 , 11 , 7 , 10 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minSumDifference ( arr , N ) ) NEW_LINE DEDENT