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 ; Prthe answer ; Driver Code ; Given array ; Size of the array ; Function call to find sum of square of differences of all possible pairs
def sumOfSquaredDifferences ( arr , N ) : NEW_LINE INDENT ans = 0 NEW_LINE sumA , sumB = 0 , 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sumA += ( arr [ i ] * arr [ i ] ) NEW_LINE sumB += arr [ i ] NEW_LINE DEDENT sumA = N * sumA NEW_LINE sumB = ( sumB * sumB ) NEW_LINE ans = sumA - sumB NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 8 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE sumOfSquaredDifferences ( arr , N ) NEW_LINE DEDENT