Sum of squares of distances between all pairs from given points | Function to find the sum of squares of distance between all distinct pairs ; Stores final answer ; Traverse the array ; Adding the effect of this polet for all the previous x - polets ; Temporarily add the square of x - coordinate ; Add the effect of this polet for all the previous y - polets ; Prlet the desired answer ; Driver code
function findSquareSum ( Coordinates , N ) { let xq = 0 , yq = 0 ; let xs = 0 , ys = 0 ; let res = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let a , b ; a = Coordinates [ i ] [ 0 ] ; b = Coordinates [ i ] [ 1 ] ; res += xq ; res -= 2 * xs * a ; res += i * ( a * a ) ; xq += a * a ; xs += a ; res += yq ; res -= 2 * ys * b ; res += i * b * b ; yq += b * b ; ys += b ; } document . write ( res ) ; } let arr = [ [ 1 , 1 ] , [ - 1 , - 1 ] , [ 1 , - 1 ] , [ - 1 , 1 ] ] ; let N = arr . length ; findSquareSum ( arr , N ) ;