Sum of width ( max and min diff ) of all Subsequences | Javascript implementation of above approach ; Function to return sum of width of all subsets ; Sort the array ; Driver program
var MOD = 1000000007 function SubseqWidths ( A , n ) { A . sort ( ( a , b ) => a - b ) var pow2 = Array ( n ) . fill ( 0 ) ; pow2 [ 0 ] = 1 ; for ( var i = 1 ; i < n ; ++ i ) pow2 [ i ] = ( pow2 [ i - 1 ] * 2 ) % MOD ; var ans = 0 ; for ( var i = 0 ; i < n ; ++ i ) ans = ( ans + ( pow2 [ i ] - pow2 [ n - 1 - i ] ) * A [ i ] ) % MOD ; return ans ; } var A = [ 5 , 6 , 4 , 3 , 8 ] ; var n = A . length ; document . write ( SubseqWidths ( A , n ) ) ;