Minimize the Sum of all the subarrays made up of the products of same | Javascript Program to implement the above approach ; Function to rearrange the second array such that the sum of its product of same indexed elements from both the arrays is minimized ; Stores ( i - 1 ) * ( n - i ) * a [ i ] for every i - th element ; Updating the value of pro according to the function ; Sort the array in reverse order ; Sort the products ; Updating the ans ; Return the ans ; Driver code ; Function call
var mod = 1000000007 ; function findMinValue ( a , b ) { var n = a . length ; var pro = Array ( n ) ; for ( var i = 0 ; i < n ; ++ i ) { pro [ i ] = ( ( i + 1 ) * ( n - i ) ) ; pro [ i ] *= ( 1 * a [ i ] ) ; ; } b . sort ( ( a , b ) => b - a ) pro . sort ( ( a , b ) => a - b ) var ans = 0 ; for ( var i = 0 ; i < n ; ++ i ) { ans += ( pro [ i ] % mod * b [ i ] ) % mod ; ans %= mod ; } return ans ; } var a = [ 1 , 2 , 3 ] ; var b = [ 2 , 3 , 2 ] ; document . write ( findMinValue ( a , b ) ) ;