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
function minSumDifference ( ar , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += ar [ i ] ; let rem = sum % n ; return rem * ( n - rem ) ; } let arr = [ 3 , 6 , 8 , 5 , 2 , 1 , 11 , 7 , 10 , 4 ] ; let N = arr . length ; document . write ( minSumDifference ( arr , N ) ) ;