Sum of squares of differences between all pairs of an array
Last Updated :
20 Oct, 2023
Given an array arr[] of size N, the task is to compute the sum of squares of differences of all possible pairs.
Examples:
Input: arr[] = {2, 8, 4}
Output: 56
Explanation:
Sum of squared differences of all possible pairs = (2 – 8)2 + (2 – 4)2 + (8 – 4)2 = 56
Input: arr[] = {-5, 8, 9, -4, -3}
Output: 950
Brute Force Approach:
The brute force approach to solve this problem is to use nested loops to generate all possible pairs of elements from the given array and then calculate the square of the difference between each pair. Finally, we can sum up all the squared differences to get the desired output.
Here are the steps of approach:
- Initialize a variable sum to 0 to keep track of the running sum of squared differences.
- Use nested loops to generate all possible pairs of elements from the given array. The outer loop will iterate through all the elements of the array, and the inner loop will start from the next element and go till the end of the array. This ensures that we generate all possible pairs of elements.
- For each pair of elements, calculate the difference between them and square the result. Add this squared difference to the running sum.
- After the loops have finished iterating through all possible pairs of elements, output the value of the sum variable. This will be the sum of squared differences of all possible pairs of elements in the given array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sumOfSquaredDifferences( int arr[], int N)
{
int sum = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int diff = arr[i] - arr[j];
sum += (diff * diff);
}
}
cout << sum << endl;
}
int main()
{
int arr[] = { 2, 8, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
sumOfSquaredDifferences(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int arr[] = { 2 , 8 , 4 };
int N = arr.length;
sumOfSquaredDifferences(arr, N);
}
public static void sumOfSquaredDifferences( int arr[],
int N)
{
int sum = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = i + 1 ; j < N; j++) {
int diff = arr[i] - arr[j];
sum += (diff * diff);
}
}
System.out.println(sum);
}
}
|
Python3
def sumOfSquaredDifferences(arr):
N = len (arr)
sum = 0
for i in range (N):
for j in range (i + 1 , N):
diff = arr[i] - arr[j]
sum + = (diff * diff)
print ( sum )
if __name__ = = "__main__" :
arr = [ 2 , 8 , 4 ]
sumOfSquaredDifferences(arr)
|
C#
using System;
class GFG
{
public static void Main( string [] args)
{
int [] arr = { 2, 8, 4 };
int N = arr.Length;
SumOfSquaredDifferences(arr, N);
}
public static void SumOfSquaredDifferences( int [] arr, int N)
{
int sum = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
int diff = arr[i] - arr[j];
sum += (diff * diff);
}
}
Console.WriteLine(sum);
}
}
|
Javascript
function sumOfSquaredDifferences(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
const diff = arr[i] - arr[j];
sum += diff * diff;
}
}
console.log(sum);
}
const arr = [2, 8, 4];
sumOfSquaredDifferences(arr);
|
Time Complexity: O(N^2) since we use nested loops to generate all possible pairs of elements.
Space Complexity: O(1) as we are not using any extra space.
Efficient Approach: The optimal idea is based on rearranging the expression in the following manner:
∑i=2 n ∑j=1i-1 (Ai-Aj)2
Since Ai – Ai = 0, the above expression can be rearranged as 1/2*(∑i=1n ∑j=1n (Ai-Aj)2) which can be further be simplified using the identity:
(A – B)2 = A2 + B2 – 2 * A * B
As 1/2*(∑i=1n ∑j=1n (Ai2 + Aj2 – 2*Ai*Aj)) = 1/2 * (2*n*∑i=1n Ai2 – 2*∑i=1n (Aj * ∑j=1n Aj))
The final expression is n*∑i=1n Ai2 – (∑i=1nAi)2 which can be computed by linearly iterating over the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sumOfSquaredDifferences( int arr[],
int N)
{
int ans = 0;
int sumA = 0, sumB = 0;
for ( int i = 0; i < N; i++) {
sumA += (arr[i] * arr[i]);
sumB += arr[i];
}
sumA = N * sumA;
sumB = (sumB * sumB);
ans = sumA - sumB;
cout << ans;
}
int main()
{
int arr[] = { 2, 8, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
sumOfSquaredDifferences(arr, N);
return 0;
}
|
Java
class GFG{
static void sumOfSquaredDifferences( int arr[],
int N)
{
int ans = 0 ;
int sumA = 0 , sumB = 0 ;
for ( int i = 0 ; i < N; i++)
{
sumA += (arr[i] * arr[i]);
sumB += arr[i];
}
sumA = N * sumA;
sumB = (sumB * sumB);
ans = sumA - sumB;
System.out.println(ans);
}
public static void main (String[] args)
{
int arr[] = { 2 , 8 , 4 };
int N = arr.length;
sumOfSquaredDifferences(arr, N);
}
}
|
Python3
def sumOfSquaredDifferences(arr, N):
ans = 0
sumA, sumB = 0 , 0
for i in range (N):
sumA + = (arr[i] * arr[i])
sumB + = arr[i]
sumA = N * sumA
sumB = (sumB * sumB)
ans = sumA - sumB
print (ans)
if __name__ = = '__main__' :
arr = [ 2 , 8 , 4 ]
N = len (arr)
sumOfSquaredDifferences(arr, N)
|
C#
using System;
class GFG{
static void sumOfSquaredDifferences( int []arr,
int N)
{
int ans = 0;
int sumA = 0, sumB = 0;
for ( int i = 0; i < N; i++)
{
sumA += (arr[i] * arr[i]);
sumB += arr[i];
}
sumA = N * sumA;
sumB = (sumB * sumB);
ans = sumA - sumB;
Console.WriteLine(ans);
}
public static void Main( string [] args)
{
int []arr = { 2, 8, 4 };
int N = arr.Length;
sumOfSquaredDifferences(arr, N);
}
}
|
Javascript
<script>
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);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...