text: Mean of array generated by products of all pairs of the given array | Function to find the mean of pair product array of arr [ ] ; Initializing suffix sum array ; Build suffix sum array ; Size of pairProductArray ; Stores sum of pairProductArray ; Store the mean ; Find mean of pairProductArray ; Return the resultant mean ; Given array arr [ ] ; Function Call
code: function pairProductMean ( arr , N ) { var suffixSumArray = Array ( N ) ; suffixSumArray [ N - 1 ] = arr [ N - 1 ] ; for ( var i = N - 2 ; i >= 0 ; i -- ) { suffixSumArray [ i ] = suffixSumArray [ i + 1 ] + arr [ i ] ; } var length = ( N * ( N - 1 ) ) / 2 ; var res = 0 ; for ( var i = 0 ; i < N - 1 ; i ++ ) { res += arr [ i ] * suffixSumArray [ i + 1 ] ; } var mean ; if ( length != 0 ) mean = res / length ; else mean = 0 ; return mean ; } var arr = [ 1 , 2 , 4 , 8 ] ; var N = arr . length ; document . write ( pairProductMean ( arr , N ) . toFixed ( 2 ) ) ;