📜  数组所有对之间的差平方和

📅  最后修改于: 2021-04-17 16:43:57             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是计算所有可能的对的差的平方和。

例子:

天真的方法:最简单的方法是生成所有可能的对,并计算每对对的差平方,然后将其继续添加到变量中,例如sum 。最后,打印sum的值

时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:最佳的想法是基于以下方式重新排列表达式:

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate sum of squares
// of differences of all possible pairs
void sumOfSquaredDifferences(int arr[],
                             int N)
{
    // Stores the final sum
    int ans = 0;
 
    // Stores temporary values
    int sumA = 0, sumB = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
        sumA += (arr[i] * arr[i]);
        sumB += arr[i];
    }
 
    sumA = N * sumA;
    sumB = (sumB * sumB);
 
    // Final sum
    ans = sumA - sumB;
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 2, 8, 4 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to find sum of square
    // of differences of all possible pairs
    sumOfSquaredDifferences(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to calculate sum of squares
// of differences of all possible pairs
static void sumOfSquaredDifferences(int arr[],
                                    int N)
{
     
    // Stores the final sum
    int ans = 0;
 
    // Stores temporary values
    int sumA = 0, sumB = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        sumA += (arr[i] * arr[i]);
        sumB += arr[i];
    }
 
    sumA = N * sumA;
    sumB = (sumB * sumB);
 
    // Final sum
    ans = sumA - sumB;
 
    // Print the answer
    System.out.println(ans);
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given array
    int arr[] = { 2, 8, 4 };
 
    // Size of the array
    int N = arr.length;
 
    // Function call to find sum of square
    // of differences of all possible pairs
    sumOfSquaredDifferences(arr, N);
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to calculate sum of squares
# of differences of all possible pairs
def sumOfSquaredDifferences(arr, N):
   
    # Stores the final sum
    ans = 0
 
    # Stores temporary values
    sumA, sumB = 0, 0
 
    # Traverse the array
    for i in range(N):
        sumA += (arr[i] * arr[i])
        sumB += arr[i]
 
    sumA = N * sumA
    sumB = (sumB * sumB)
 
    # Final sum
    ans = sumA - sumB
 
    # Prthe answer
    print(ans)
 
# Driver Code
if __name__ == '__main__':
   
    # Given array
    arr = [2, 8, 4]
 
    # Size of the array
    N = len(arr)
     
    # Function call to find sum of square
    # of differences of all possible pairs
    sumOfSquaredDifferences(arr, N)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to calculate sum of squares
// of differences of all possible pairs
static void sumOfSquaredDifferences(int []arr,
                                    int N)
{
     
    // Stores the final sum
    int ans = 0;
 
    // Stores temporary values
    int sumA = 0, sumB = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        sumA += (arr[i] * arr[i]);
        sumB += arr[i];
    }
 
    sumA = N * sumA;
    sumB = (sumB * sumB);
 
    // Final sum
    ans = sumA - sumB;
 
    // Print the answer
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given array
    int []arr = { 2, 8, 4 };
 
    // Size of the array
    int N = arr.Length;
 
    // Function call to find sum of square
    // of differences of all possible pairs
    sumOfSquaredDifferences(arr, N);
}
}
 
// This code is contributed by AnkThon


Javascript


输出:
56

时间复杂度: O(N)
辅助空间: O(1)