给定一个由XY平面上N个点的坐标组成的数组arr [] ,任务是找到所有成对点之间的距离的平方和,即(X i – X j ) 2 +(Y i – Y j ) 2对于每个不同的对(i,j) 。
例子:
Input: arr[][] = {{1, 1}, {-1, -1}, {1, -1}, {-1, 1}}
Output: 32
Explanation:
Distance of 1st point (1, 1) from the 2nd, 3rd and 4th points are 8, 4 and 4 respectively.
Distance of 2nd point from the 3rd and 4th points are 4 and 4 respectively.
Distance of 3rd point from the 4th point is 8.
Therefore, the total distance = (8 + 4 + 4) + (4 + 4) + (8) = 32
Input: arr[][] = {{1, 1}, {1, 1}, {0, 0}}
Output: 4
Explanation:
Distance of 1st point from the 2nd and 3rd points are 0 and 2 respectively.
Distance of 2nd point from the 3rd point is 2.
Therefore, the total distance = (0 + 2) + (2) = 4
天真的方法:解决问题的最简单方法是生成给定数组arr [] []的所有可能的不同对,并计算所有点对(X i ,Y j )和(X j ,Y j ),即(X i – X j ) 2 +(Y i – Y j ) 2 ,对于每个不同的对(i,j) 。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:为了优化上述方法,我们的想法是重新组合和并将距离的平方和分成两个和。请按照以下步骤解决问题:
- 初始化变量,例如xq , yq , xs和ys 。
- 用零初始化一个变量,例如res ,以存储结果总和。
- 遍历给定的数组,并对每个点{x,y} ,执行以下步骤:
- 在变量res中添加(i * x 2 + i * y 2 )的值,这对应于平方距离的相加。
- 将值(xq – 2 * xs * a)和(yq – 2 * ys * b)相加,以消除(*)在扩展(a – b) 2时2 * X * Y的影响。
- 将值a 2和b 2分别添加到变量xq和yq 。
- 将值a和b分别添加到变量xs和ys中。
- 将xs和yq值分别添加到变量a 2和b 2中。
- 完成上述步骤后,打印res的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the sum of squares
// of distance between all distinct pairs
void findSquareSum(
int Coordinates[][2], int N)
{
long long xq = 0, yq = 0;
long long xs = 0, ys = 0;
// Stores final answer
long long res = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
int a, b;
a = Coordinates[i][0];
b = Coordinates[i][1];
res += xq;
res -= 2 * xs * a;
// Adding the effect of this
// point for all the previous
// x - points
res += i * (long long)(a * a);
// Temporarily add the
// square of x-coordinate
xq += a * a;
xs += a;
res += yq;
res -= 2 * ys * b;
res += i * (long long)b * b;
// Add the effect of this point
// for all the previous y - points
yq += b * b;
ys += b;
}
// Print the desired answer
cout << res;
}
// Driver Code
int main()
{
int arr[][2] = { { 1, 1 },
{ -1, -1 },
{ 1, -1 },
{ -1, 1 } };
int N = sizeof(arr) / sizeof(arr[0]);
findSquareSum(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to find the sum of squares
// of distance between all distinct pairs
static void findSquareSum(
int Coordinates[][], int N)
{
long xq = 0, yq = 0;
long xs = 0, ys = 0;
// Stores final answer
long res = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
int a, b;
a = Coordinates[i][0];
b = Coordinates[i][1];
res += xq;
res -= 2 * xs * a;
// Adding the effect of this
// point for all the previous
// x - points
res += i * (long)(a * a);
// Temporarily add the
// square of x-coordinate
xq += a * a;
xs += a;
res += yq;
res -= 2 * ys * b;
res += i * (long)b * b;
// Add the effect of this point
// for all the previous y - points
yq += b * b;
ys += b;
}
// Print the desired answer
System.out.println(res);
}
// Driver Code
public static void main(String[] args)
{
int arr[][] = { { 1, 1 },
{ -1, -1 },
{ 1, -1 },
{ -1, 1 } };
int N = arr.length;
findSquareSum(arr, N);
}
}
// This code is contributed by code_hunt.
Python3
# Python3 program for the above approach
# Function to find the sum of squares
# of distance between all distinct pairs
def findSquareSum(Coordinates, N):
xq , yq = 0, 0
xs , ys = 0, 0
# Stores final answer
res = 0
# Traverse the array
for i in range(N):
a = Coordinates[i][0]
b = Coordinates[i][1]
res += xq
res -= 2 * xs * a
# Adding the effect of this
# point for all the previous
# x - points
res += i * (a * a)
# Temporarily add the
# square of x-coordinate
xq += a * a
xs += a
res += yq
res -= 2 * ys * b
res += i * b * b
# Add the effect of this point
# for all the previous y - points
yq += b * b
ys += b
# Print the desired answer
print (res)
# Driver Code
if __name__ == '__main__':
arr = [ [ 1, 1 ],
[ -1, -1 ],
[ 1, -1 ],
[ -1, 1 ] ]
N = len(arr)
findSquareSum(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the sum of squares
// of distance between all distinct pairs
static void findSquareSum(int[,] Coordinates, int N)
{
long xq = 0, yq = 0;
long xs = 0, ys = 0;
// Stores final answer
long res = 0;
// Traverse the array
for(int i = 0; i < N ; i++)
{
int a, b;
a = Coordinates[i, 0];
b = Coordinates[i, 1];
res += xq;
res -= 2 * xs * a;
// Adding the effect of this
// point for all the previous
// x - points
res += i * (long)(a * a);
// Temporarily add the
// square of x-coordinate
xq += a * a;
xs += a;
res += yq;
res -= 2 * ys * b;
res += i * (long)b * b;
// Add the effect of this point
// for all the previous y - points
yq += b * b;
ys += b;
}
// Print the desired answer
Console.Write(res);
}
// Driver code
static void Main()
{
int[,] arr = { { 1, 1 },
{ -1, -1 },
{ 1, -1 },
{ -1, 1 } };
int N = arr.GetLength(0);
findSquareSum(arr, N);
}
}
// This code is contributed by code_hunt
32
时间复杂度: O(N)
辅助空间: O(1)