Sum of squares of differences between all pairs of an array | Function to calculate sum of squares of differences of all possible pairs ; Stores the final sum ; Stores temporary values ; Traverse the array ; Final sum ; Print the answer ; Given array ; Size of the array ; Function call to find sum of square of differences of all possible pairs
function sumOfSquaredDifferences ( arr , N ) { let ans = 0 ; let sumA = 0 , sumB = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sumA += ( arr [ i ] * arr [ i ] ) ; sumB += arr [ i ] ; } sumA = N * sumA ; sumB = ( sumB * sumB ) ; ans = sumA - sumB ; document . write ( ans ) ; } let arr = [ 2 , 8 , 4 ] ; let N = arr . length ; sumOfSquaredDifferences ( arr , N ) ;