Minimize the sum of product of two arrays with permutations allowed | Returns minimum sum of product of two arrays with permutations allowed ; Sort A and B so that minimum and maximum value can easily be fetched . ; Multiplying minimum value of A and maximum value of B ; Driver Code
< ? php function minValue ( $ A , $ B , $ n ) { sort ( $ A ) ; sort ( $ A , $ n ) ; sort ( $ B ) ; sort ( $ B , $ n ) ; $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ result += ( $ A [ $ i ] * $ B [ $ n - $ i - 1 ] ) ; return $ result ; } $ A = array ( 3 , 1 , 1 ) ; $ B = array ( 6 , 5 , 4 ) ; $ n = sizeof ( $ A ) / sizeof ( $ A [ 0 ] ) ; echo minValue ( $ A , $ B , $ n ) ; ? >