📜  所有数组元素对的乘积之和

📅  最后修改于: 2022-05-13 01:57:49.126000             🧑  作者: Mango

所有数组元素对的乘积之和

给定一个整数数组 A[] 求所有数组元素对的乘积之和,即我们需要在执行以下伪代码后求乘积

product = 0
for i = 1:n
    for j = i+1:n
        product = product + A[i]*A[j]

例子:

Input : A[] = {1, 3, 4}
Output : 19
Possible Pairs : (1,3), (1,4), (3,4)
Sum of Product : 1*3 + 1*4 + 3*4 = 19

天真的解决方案:

对于每个索引 i,我们循环 j=i+1 到 j=n 并每次添加 A[i]*A[j]。下面是相同的实现。

C++
// A naive C++ program to find sum of product
#include 
using namespace std;
 
// Returns sum of pair products
int findProductSum(int A[], int n)
{
    int product = 0;
    for (int i = 0; i < n; i++)
        for (int j = i+1; j < n; j++)
            product = product + A[i]*A[j];
    return product;
}
 
// Driver code
int main()
{
    int A[] = {1, 3, 4};
    int n = sizeof(A)/sizeof(A[0]);
 
    cout << "sum of product of all pairs "
    "of array elements : " << findProductSum(A, n);
 
    return 0;
}


Java
/*package whatever //do not write package name here */
// A naive Java program to find sum of product
import java.io.*;
class test
{
    // Returns sum of pair products
    int findProductSum(int A[], int n)
    {
    int product = 0;
    for (int i = 0; i < n; i++)
        for (int j = i+1; j < n; j++)
            product = product + A[i]*A[j];
    return product;
    }
}
class GFG {
 
// Driver code
    public static void main (String[] args) {
 
    int A[] = {1, 3, 4};
    int n = A.length;
    test t = new test();
    System.out.print("sum of product of all pairs of array elements : ");
    System.out.println(t.findProductSum(A, n));
 
    }
}


Python3
# A naive python3 program to find sum of product
  
# Returns sum of pair products
def findProductSum(A,n):
 
    product = 0
    for i in range (n):
        for j in range ( i+1,n):
            product = product + A[i]*A[j]
    return product
  
# Driver code
if __name__=="__main__":
 
    A = [1, 3, 4]
    n = len (A)
  
    print("sum of product of all pairs "
    "of array elements : " ,findProductSum(A, n))


C#
// A naive C# program to find sum of product
using System;
 
class GFG
{
     
// Returns sum of pair products
static int findProductSum(int[] A, int n)
{
    int product = 0;
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            product = product + A[i] * A[j];
    return product;
}
 
// Driver code
public static void Main()
{
    int[] A = {1, 3, 4};
    int n = A.Length;
    Console.WriteLine("sum of product of all " +
                      "pairs of array elements : ");
    Console.WriteLine(findProductSum(A, n));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


C++
// Efficient C++ program to find sum pair products
// in an array.
#include 
using namespace std;
 
// required function
int findProductSum(int A[], int n)
{
    // calculating array sum (a1 + a2  ... + an)
    int array_sum = 0;
    for (int i = 0; i < n; i++)
        array_sum = array_sum + A[i];
 
    // calculating square of array sum
    // (a1 + a2 + ... + an)^2
    int array_sum_square = array_sum * array_sum;
 
    // calculating a1^2 + a2^2 + ... + an^2
    int individual_square_sum = 0;
    for (int i = 0; i < n; i++)
        individual_square_sum += A[i]*A[i];
 
    // required sum is (array_sum_square -
    // individual_square_sum) / 2
    return (array_sum_square - individual_square_sum)/2;
}
 
// Driver code
int main()
{
    int A[] = {1, 3, 4};
    int n = sizeof(A)/sizeof(A[0]);
    cout << "sum of product of all pairs of array "
            "elements : " << findProductSum(A, n);
    return 0;
}


Java
// Efficient Java program to find sum pair products
// in an array.
class GFG
{
 
// required function
static int findProductSum(int A[], int n)
{
    // calculating array sum (a1 + a2 ... + an)
    int array_sum = 0;
    for (int i = 0; i < n; i++)
        array_sum = array_sum + A[i];
 
    // calculating square of array sum
    // (a1 + a2 + ... + an)^2
    int array_sum_square = array_sum * array_sum;
 
    // calculating a1^2 + a2^2 + ... + an^2
    int individual_square_sum = 0;
    for (int i = 0; i < n; i++)
        individual_square_sum += A[i] * A[i];
 
    // required sum is (array_sum_square -
    // individual_square_sum) / 2
    return (array_sum_square - individual_square_sum) / 2;
}
 
// Driver code
public static void main(String[] args)
{
    int A[] = {1, 3, 4};
    int n = A.length;
    System.out.println("sum of product of all pairs of array "
            +"elements : " + findProductSum(A, n));
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Efficient python 3 program to find sum
# pair products in an array.
 
# required function
def findProductSum(A, n):
     
    # calculating array sum (a1 + a2 ... + an)
    array_sum = 0
    for i in range(0, n, 1):
        array_sum = array_sum + A[i]
 
    # calculating square of array sum
    # (a1 + a2 + ... + an)^2
    array_sum_square = array_sum * array_sum
 
    # calculating a1^2 + a2^2 + ... + an^2
    individual_square_sum = 0
    for i in range(0, n, 1):
        individual_square_sum += A[i] * A[i]
 
    # required sum is (array_sum_square -
    # individual_square_sum) / 2
    return (array_sum_square -
            individual_square_sum) / 2
 
# Driver code
if __name__ == '__main__':
    A = [1, 3, 4]
    n = len(A)
    print("sum of product of all pairs of",
          "array elements :", int(findProductSum(A, n)))
     
# This code is contributed by
# Sahil_Shelangia


C#
// Efficient C# program to find sum pair
// products in an array.
using System;
 
class GFG
{
 
// required function
static int findProductSum(int[] A, int n)
{
    // calculating array sum (a1 + a2 ... + an)
    int array_sum = 0;
    for (int i = 0; i < n; i++)
        array_sum = array_sum + A[i];
 
    // calculating square of array sum
    // (a1 + a2 + ... + an)^2
    int array_sum_square = array_sum * array_sum;
 
    // calculating a1^2 + a2^2 + ... + an^2
    int individual_square_sum = 0;
    for (int i = 0; i < n; i++)
        individual_square_sum += A[i] * A[i];
 
    // required sum is (array_sum_square -
    // individual_square_sum) / 2
    return (array_sum_square -
            individual_square_sum) / 2;
}
 
// Driver code
public static void Main()
{
    int[] A = {1, 3, 4};
    int n = A.Length;
    Console.WriteLine("sum of product of all " +
                      "pairs of array elements : " +
                       findProductSum(A, n));
}
}
 
// This code is contributed by Akanksha Rai


PHP


Javascript


输出:

sum of product of all pairs of array elements : 19

时间复杂度: O(n 2 )
空间复杂度: O(1)

高效的 O(n) 解决方案:

We know that
(a + b + c)2 = a2 + b2 + c2 + 2*(a*b + b*c + c*a)
Let required sum be P
Let E = (a1 + a2 + a3 + a4 ... + an)^2 
=> E = a12 + a22 + ... + an2 + 2*(a1*a2 + a1*a3 + ....)
=> E = a12 + a22 + ... + an2 + 2*(P)
=> P = ( E - (a12 + a22 + .... + an2) ) / 2

C++

// Efficient C++ program to find sum pair products
// in an array.
#include 
using namespace std;
 
// required function
int findProductSum(int A[], int n)
{
    // calculating array sum (a1 + a2  ... + an)
    int array_sum = 0;
    for (int i = 0; i < n; i++)
        array_sum = array_sum + A[i];
 
    // calculating square of array sum
    // (a1 + a2 + ... + an)^2
    int array_sum_square = array_sum * array_sum;
 
    // calculating a1^2 + a2^2 + ... + an^2
    int individual_square_sum = 0;
    for (int i = 0; i < n; i++)
        individual_square_sum += A[i]*A[i];
 
    // required sum is (array_sum_square -
    // individual_square_sum) / 2
    return (array_sum_square - individual_square_sum)/2;
}
 
// Driver code
int main()
{
    int A[] = {1, 3, 4};
    int n = sizeof(A)/sizeof(A[0]);
    cout << "sum of product of all pairs of array "
            "elements : " << findProductSum(A, n);
    return 0;
}

Java

// Efficient Java program to find sum pair products
// in an array.
class GFG
{
 
// required function
static int findProductSum(int A[], int n)
{
    // calculating array sum (a1 + a2 ... + an)
    int array_sum = 0;
    for (int i = 0; i < n; i++)
        array_sum = array_sum + A[i];
 
    // calculating square of array sum
    // (a1 + a2 + ... + an)^2
    int array_sum_square = array_sum * array_sum;
 
    // calculating a1^2 + a2^2 + ... + an^2
    int individual_square_sum = 0;
    for (int i = 0; i < n; i++)
        individual_square_sum += A[i] * A[i];
 
    // required sum is (array_sum_square -
    // individual_square_sum) / 2
    return (array_sum_square - individual_square_sum) / 2;
}
 
// Driver code
public static void main(String[] args)
{
    int A[] = {1, 3, 4};
    int n = A.length;
    System.out.println("sum of product of all pairs of array "
            +"elements : " + findProductSum(A, n));
    }
}
 
// This code is contributed by 29AjayKumar

Python3

# Efficient python 3 program to find sum
# pair products in an array.
 
# required function
def findProductSum(A, n):
     
    # calculating array sum (a1 + a2 ... + an)
    array_sum = 0
    for i in range(0, n, 1):
        array_sum = array_sum + A[i]
 
    # calculating square of array sum
    # (a1 + a2 + ... + an)^2
    array_sum_square = array_sum * array_sum
 
    # calculating a1^2 + a2^2 + ... + an^2
    individual_square_sum = 0
    for i in range(0, n, 1):
        individual_square_sum += A[i] * A[i]
 
    # required sum is (array_sum_square -
    # individual_square_sum) / 2
    return (array_sum_square -
            individual_square_sum) / 2
 
# Driver code
if __name__ == '__main__':
    A = [1, 3, 4]
    n = len(A)
    print("sum of product of all pairs of",
          "array elements :", int(findProductSum(A, n)))
     
# This code is contributed by
# Sahil_Shelangia

C#

// Efficient C# program to find sum pair
// products in an array.
using System;
 
class GFG
{
 
// required function
static int findProductSum(int[] A, int n)
{
    // calculating array sum (a1 + a2 ... + an)
    int array_sum = 0;
    for (int i = 0; i < n; i++)
        array_sum = array_sum + A[i];
 
    // calculating square of array sum
    // (a1 + a2 + ... + an)^2
    int array_sum_square = array_sum * array_sum;
 
    // calculating a1^2 + a2^2 + ... + an^2
    int individual_square_sum = 0;
    for (int i = 0; i < n; i++)
        individual_square_sum += A[i] * A[i];
 
    // required sum is (array_sum_square -
    // individual_square_sum) / 2
    return (array_sum_square -
            individual_square_sum) / 2;
}
 
// Driver code
public static void Main()
{
    int[] A = {1, 3, 4};
    int n = A.Length;
    Console.WriteLine("sum of product of all " +
                      "pairs of array elements : " +
                       findProductSum(A, n));
}
}
 
// This code is contributed by Akanksha Rai

PHP


Javascript


输出:

sum of product of all pairs of array elements : 19