Sum of product of all pairs of array elements
Last Updated :
19 Sep, 2023
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
Naive Solution :
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++
// A naive C++ program to find sum of product
#include <iostream>
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
<?php
// A naive PHP program to find
// sum of product
// Returns sum of pair products
function 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
?>
JavaScript
<script>
// A naive Javascript program to find sum of product
// Returns sum of pair products
function 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)
Efficient O(n) solution :
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 <iostream>
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
<?php
// Efficient PHP program to find sum
// pair products in an array.
// required function
function 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
?>
JavaScript
<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)
Another Efficient Solution:
For large numbers when we should also work with modulus of 10^9+7. The above approach may not work. So the Intuition for this approach is for each number if we multiply it with prefix sum then all pair before the element are covered and then element is added to the sum which will guarantee us with the multiplication of pairs after the element.
Examples:
Input : A[] = {1, 3, 4, 5}
Output : 59
Possible Pairs : (1,3), (1,4), (1,5), (3,4), (3,5), (4,5)
Sum of Product : 1*3 + 1*4 + 1*5 + 3*4 + 3*5 +4*5 = 59
Intuition:
Initially ans=0, sum=0, i=0.so,
i=k :
ans+=sum*A[k];
sum+=A[k];
--------------------------------------
i=0 :A[i]=1
ans+=(0)*(1); ans==0
sum+=1; sum==1 (1)
i=1 : A[i]=3
ans+=(1)*(3); ans==3
sum+=3; sum==4 (1+3)
i=2 : A[i]=4
ans+=(4)*(4); ans==19
sum+=4; sum==8 (1+3+4)
i=3 : A[i]=5
ans+=(8)*(5); ans==59
sum+=5; sum==13 (1+3+4+5)
So, ans=59.
C++
// Efficient C++ program to find sum pair products
// in an array.
#include <iostream>
using namespace std;
// required function
int findProductSum(int A[], int N)
{
long long ans = 0;
long long sum = 0;
long long Mod = 1000000007;
for (int i = 0; i < N; i++) {
ans += (sum * A[i]) % Mod;
ans %= Mod;
sum += A[i];
sum %= Mod;
}
return ans;
}
// 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;
}
// This code is contributed by Kasina Dheeraj.
Java
// Efficient Java program to find sum pair products in an
// array.
import java.io.*;
class GFG {
// required function
static int findProductSum(int[] A, int N)
{
long ans = 0;
long sum = 0;
long Mod = 1000000007;
for (int i = 0; i < N; i++) {
ans += (sum * A[i]) % Mod;
ans %= Mod;
sum += A[i];
sum %= Mod;
}
return (int)ans;
}
public static void main(String[] args)
{
int[] A = { 1, 3, 4 };
int n = A.length;
System.out.print(
"Sum of product of all pairs of array elements : "
+ findProductSum(A, n));
}
}
// This code is contributed by lokeshmvs21.
Python3
# Efficient python program to find sum pair products in an array.
# Required function
def findProductSum(A, N):
ans = 0
Sum = 0
Mod = 1000000007
for i in range(N):
ans += (Sum * A[i]) % Mod
ans %= Mod
Sum += A[i]
Sum %= Mod
return ans
A = [1, 3, 4]
n = len(A)
print("Sum of product of all pairs of array elements :", findProductSum(A, n))
# This code is contributed by lokeshmvs21.
C#
// Efficient C# program to find sum pair products in an
// array.
using System;
public class GFG
{
// required function
static int findProductSum(int[] A, int N)
{
long ans = 0;
long sum = 0;
long Mod = 1000000007;
for (int i = 0; i < N; i++) {
ans += (sum * A[i]) % Mod;
ans %= Mod;
sum += A[i];
sum %= Mod;
}
return (int)ans;
}
static public void Main()
{
// Code
int[] A = { 1, 3, 4 };
int n = A.Length;
Console.Write(
"Sum of product of all pairs of array elements : "
+ findProductSum(A, n));
}
}
// This code is contributed by lokeshmvs21.
JavaScript
class GFG
{
// required function
static findProductSum(A, N)
{
var ans = 0;
var sum = 0;
var Mod = 1000000007;
for (var i=0; i < N; i++)
{
ans += (sum * A[i]) % Mod;
ans %= Mod;
sum += A[i];
sum %= Mod;
}
return parseInt(ans);
}
static main(args)
{
var A = [1, 3, 4];
var n = A.length;
console.log("Sum of product of all pairs of array elements : " + GFG.findProductSum(A, n));
}
}
GFG.main([]);
// This code is contributed by dhanshriborse561
Output:
Sum of product of all pairs of array elements : 19
Time Complexity : O(n)
Auxiliary Space : O(1)
Similar Reads
Sum of product of all pairs of a Binary Array Given a binary array arr[] of size N, the task is to print the sum of product of all pairs of the given array elements. Note: The binary array contains only 0 and 1. Examples: Input: arr[] = {0, 1, 1, 0, 1}Output: 3Explanation: Sum of product of all possible pairs are: {0 Ã 1 + 0 Ã 1 + 0 Ã 0 + 0 Ã 1
5 min read
Sum of the Product of digits of all Array elements Given an array arr, the task is to find the sum of the product of digits of all array elements Example: Input: arr[]={11, 23, 41}Output: 11Explanation: 1*1 + 2*3 + 4*1 = 1 + 6 + 4 = 1111 Input: arr[]={46, 32, 78, 0}Output: 86 Approach: To solve this problem, find the product of digits of all numbers
4 min read
Sum of product of all elements of sub-arrays of size k Given an array and a number k, the task is to calculate the sum of the product of all elements of subarrays of size k. Examples : Input : arr[] = {1, 2, 3, 4, 5, 6} k = 3 Output : 210 Consider all subarrays of size k 1*2*3 = 6 2*3*4 = 24 3*4*5 = 60 4*5*6 = 120 6 + 24 + 60 + 120 = 210 Input : arr[] =
11 min read
Minimum sum of product of elements of pairs of the given array Given an array arr[] of even number of element N in it. The task is to form N/2 pairs such that sum of product of elements in those pairs is minimum. Examples Input: arr[] = { 1, 6, 3, 1, 7, 8 } Output: 270 Explanation: The pair formed are {1, 1}, {3, 6}, {7, 8} Product of sum of these pairs = 2 * 9
5 min read
Find the sum of all possible pairs in an array of N elements Given an array arr[] of N integers, the task is to find the sum of all the pairs possible from the given array. Note that, (arr[i], arr[i]) is also considered as a valid pair.(arr[i], arr[j]) and (arr[j], arr[i]) are considered as two different pairs.Examples: Input: arr[] = {1, 2} Output: 12 All va
7 min read
Sum of all ordered pair-products from a given array Given an array arr[] of size N, the task is to find the sum of all products of ordered pairs that can be generated from the given array elements.Examples: Input: arr[] ={1, 2, 3}Output: 36Explanation:All possible pairs are {(1, 1), {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}}. The
4 min read
Product of all pairwise consecutive elements in an Array Given an array of integers of N elements. The task is to print the product of all of the pairwise consecutive elements.Pairwise consecutive pairs of an array of size N are (a[i], a[i+1]) for all i ranging from 0 to N-2 Examples: Input : arr[] = {8, 5, 4, 3, 15, 20} Output : 40, 20, 12, 45, 300 Input
4 min read
Mean of array generated by products of all pairs of the given array Given an array arr[] consisting of N integers, the task is to find the mean of the array formed by the products of unordered pairs of the given array. Examples: Input: arr[] = {2, 5, 7}Output: 19.67Explanation:Product of unordered pairs of array arr[] are 2 * 5 = 10, 2 * 7 = 14 and 5 * 7 = 35.Theref
12 min read
XOR of Sum of every possible pair of an array Given an array A of size n. the task is to generate a new sequence B with size N^2 having elements sum of every pair of array A and find the xor value of the sum of all the pairs formed. Note: Here (A[i], A[i]), (A[i], A[j]), (A[j], A[i]) all are considered as different pairs. Examples: Input: arr[]
5 min read
Product of all the pairs from the given array Given an array arr[] of N integers, the task is to find the product of all the pairs possible from the given array such as: (arr[i], arr[i]) is also considered as a valid pair.(arr[i], arr[j]) and (arr[j], arr[i]) are considered as two different pairs. Print the resultant answer modulus 10^9+7. Exam
11 min read