Minimum sum of differences with an element in an array | Function to find min sum after operation ; Sort the array ; Pick middle value ; Sum of absolute differences . ; Driver Code
function absSumDidd ( a ) { a . sort ( ( a , b ) => a - b ) ; var midValue = a [ a . length / 2 ] ; var sum = 0 ; for ( var i = 0 ; i < a . length ; i ++ ) { sum = sum + Math . abs ( a [ i ] - midValue ) ; } return sum ; } var arr = [ 5 , 11 , 14 , 10 , 17 , 15 ] ; document . write ( absSumDidd ( arr ) ) ;