Sum of product of all pairs of array elements

14 Aug, 2021
Given an array A[] of integers find sum of product of all pairs of array elements i. e., we need to find of product after execution of following pseudo code 
product = 0
for i = 1:n
    for j = i+1:n
        product = product + A[i]*A[j]
Examples: 
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
 
 
For each index i we loop through j=i+1 to j=n and add A[i]*A[j] each time. Below is implementation for the same.  
C++
Java
Python3
C#
PHP
Javascript
// A naive C++ program to find sum of product#include <iostream>using namespace std; // Returns sum of pair productsint 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 codeint 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;}
/*package whatever //do not write package name here */// A naive Java program to find sum of productimport 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));     }}
# A naive python3 program to find sum of product  # Returns sum of pair productsdef 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 codeif __name__=="__main__":     A = [1, 3, 4]    n = len (A)      print("sum of product of all pairs "    "of array elements : " ,findProductSum(A, n))
// A naive C# program to find sum of productusing System; class GFG{     // Returns sum of pair productsstatic 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 codepublic 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// A naive PHP program to find// sum of product // Returns sum of pair productsfunction findProductSum($A, $n){    $product = 0;    for ($i = 0; $i < $n; $i++)        for ($j = $i + 1; $j < $n; $j++)            $product = $product + $A[$i] * $A[$j];    return $product;}     // Driver code    $A = array (1, 3, 4);    $n = sizeof($A);     echo "sum of product of all pairs ",         "of array elements : "         ,findProductSum($A, $n); // This code is contributed by aj_36?>
<script> // A naive Javascript program to find sum of product   // Returns sum of pair productsfunction findProductSum(A, n){    let product = 0;    for (let i= 0; i < n; i++)        for (let j = i+1; j < n; j++)            product = product + A[i]*A[j];    return product;}   // Driver code    let A = [1, 3, 4];    let n = A.length;       document.write("sum of product of all pairs " +    "of array elements : " + findProductSum(A, n));     // This code is contributed by Mayank Tyagi   </script>
Output: 
sum of product of all pairs of array elements : 19
Time Complexity : O(n2) Space Complexity : O(1) 
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++
Java
Python3
C#
PHP
Javascript
// Efficient C++ program to find sum pair products// in an array.#include <iostream>using namespace std; // required functionint 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 codeint 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;}
// Efficient Java program to find sum pair products// in an array.class GFG{ // required functionstatic 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 codepublic 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
# Efficient python 3 program to find sum# pair products in an array. # required functiondef 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 codeif __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
// Efficient C# program to find sum pair// products in an array.using System; class GFG{ // required functionstatic 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 codepublic 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// Efficient PHP program to find sum// pair products in an array. // required functionfunction findProductSum(&$A, $n){    // calculating array sum (a1 + a2 ... + an)    $array_sum = 0;    for ($i = 0; $i < $n; $i++)        $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 = 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$A = array(1, 3, 4);$n = sizeof($A);echo("sum of product of all pairs " .             "of array elements : ");echo (findProductSum($A, $n)); // This code is contributed// by Shivi_Aggarwal?>
<script>    // Efficient Javascript program to find sum pair    // products in an array.         // required function    function findProductSum(A, n)    {        // calculating array sum (a1 + a2 ... + an)        let array_sum = 0;        for (let i = 0; i < n; i++)            array_sum = array_sum + A[i];         // calculating square of array sum        // (a1 + a2 + ... + an)^2        let array_sum_square = array_sum * array_sum;         // calculating a1^2 + a2^2 + ... + an^2        let individual_square_sum = 0;        for (let 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;    }         let A = [1, 3, 4];    let n = A.length;    document.write("sum of product of all " +                      "pairs of array elements : " +                       findProductSum(A, n));         // This code is contributed by rameshtravel07.</script>
Output: 
sum of product of all pairs of array elements : 19
Time Complexity : O(n) Space Complexity : O(1)This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
jit_t
ukasp
29AjayKumar
sahilshelangia
Akanksha_Rai
princiraj1992
Shivi_Aggarwal
mayanktyagi1709
rameshtravel07
saurabh1990aror
surindertarika1234
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.