Given an array arr[] of n integers. The task is to find the sum of product of each element with each element after it in the array. In other words, find sum of product of each arr[i] with each arr[j] such that j > i.
Examples :
Input : arr[] = {9, 3, 4, 2}
Output : 107
Explanation:
Sum of product of arr[0] with arr[1], arr[2], arr[3] is 9*3 + 9*4 + 9*2 = 81
Sum of product of arr[1] with arr[2], arr[3] is 3*4 + 3*2 = 18
Product of arr[2] with arr[3] is 4*2 = 8
Sum of all these sums is 81 + 18 + 8 = 107
Input : arr[] = {3, 5}
Output : 15
Observe to find (p*a + p*b + …..+ p*y + p*z) is equals to p*(a + b ….. y + z).
So for each i, 0 ? i ? n – 2 we have to calculate arr[i] * (arr[j] + arr[j+1] + ….. + arr[n – 2] + arr[n – 1]), where j = i + 1 and find sum of these.
We can reduce the complexity of finding (arr[j] + arr[j+1] + ….. + arr[n – 2] + arr[n – 1]) for each ‘i’ by precomputing the suffix sum of the array.
Lets sum[] stores the suffix sum of the given array i.e sum[i] have arr[i] + arr[i+1] + ….. + arr[n – 2] + arr[n – 1].
So, after precompute sum[], we need to find the sum of arr[i]*sum[i + 1] where 0 ? i ? n – 2.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSumofProduct( int arr[], int n)
{
int sum[n];
sum[n - 1] = arr[n - 1];
for ( int i = n - 2; i >= 0; i--)
sum[i] = sum[i + 1] + arr[i];
int res = 0;
for ( int i = 0; i < n - 1; i++)
res += (sum[i + 1] * arr[i]);
return res;
}
int main()
{
int arr[] = {9, 3, 4, 2};
int n = sizeof (arr) / sizeof (arr[0]);
cout << findSumofProduct(arr, n)
<< endl;
return 0;
}
|
Java
class GFG {
static int findSumofProduct( int arr[],
int n)
{
int sum[] = new int [n];
sum[n - 1 ] = arr[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; i--)
sum[i] = sum[i + 1 ] + arr[i];
int res = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
res += (sum[i + 1 ] * arr[i]);
return res;
}
public static void main(String[] args)
{
int arr[] = { 9 , 3 , 4 , 2 };
int n = arr.length;
System.out.println(
findSumofProduct(arr, n));
}
}
|
Python3
def findSumofProduct(arr, n):
sum = [ None for _ in range (n)]
sum [n - 1 ] = arr[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
sum [i] = sum [i + 1 ] + arr[i]
res = 0
for i in range (n - 1 ):
res + = sum [i + 1 ] * arr[i]
return res
arr = [ 9 , 3 , 4 , 2 ]
n = len (arr)
print (findSumofProduct(arr, n))
|
C#
using System;
class GFG {
static int findSumofProduct( int [] arr,
int n)
{
int [] sum = new int [n];
sum[n - 1] = arr[n - 1];
for ( int i = n - 2; i >= 0; i--)
sum[i] = sum[i + 1] + arr[i];
int res = 0;
for ( int i = 0; i < n - 1; i++)
res += (sum[i + 1] * arr[i]);
return res;
}
static public void Main ()
{
int [] arr = { 9, 3, 4, 2 };
int n = arr.Length;
Console.WriteLine(findSumofProduct(arr, n));
}
}
|
PHP
<?php
function findSumofProduct( $arr , $n )
{
$sum = array ();
$sum [ $n - 1] = $arr [ $n - 1];
for ( $i = $n - 2; $i >= 0; $i --)
$sum [ $i ] = $sum [ $i + 1] + $arr [ $i ];
$res = 0;
for ( $i = 0; $i < $n - 1; $i ++)
$res += ( $sum [ $i + 1] * $arr [ $i ]);
return $res ;
}
$arr = array (9, 3, 4, 2);
$n = count ( $arr );
echo findSumofProduct( $arr , $n );
?>
|
Javascript
<script>
function findSumofProduct(arr, n)
{
let sum = new Array(n);
sum[n - 1] = arr[n - 1];
for (let i = n - 2; i >= 0; i--)
sum[i] = sum[i + 1] + arr[i];
let res = 0;
for (let i = 0; i < n - 1; i++)
res += (sum[i + 1] * arr[i]);
return res;
}
let arr = [9, 3, 4, 2];
let n = arr.length;
document.write(findSumofProduct(arr, n)
+ "<br>" );
</script>
|
Time complexity: O(n) where n is the size of the given array
Auxiliary space: O(n)
Below is Optimized Code of finding sum of product of each element with each element after it:
C++
#include <bits/stdc++.h>
using namespace std;
int findSumofProduct( int arr[],
int n)
{
int suffix_sum = arr[n - 1];
int res = 0;
for ( int i = n - 2; i >= 0; i--)
{
res += (suffix_sum * arr[i]);
suffix_sum += arr[i];
}
return res;
}
int main()
{
int arr[] = {9, 3, 4, 2};
int n = sizeof (arr) / sizeof (arr[0]);
cout << findSumofProduct(arr, n)
<< endl;
return 0;
}
|
Java
import java .io.*;
class GFG {
static int findSumofProduct( int [] arr,
int n)
{
int suffix_sum = arr[n - 1 ];
int res = 0 ;
for ( int i = n - 2 ; i >= 0 ; i--)
{
res += (suffix_sum * arr[i]);
suffix_sum += arr[i];
}
return res;
}
static public void main(String[] args)
{
int [] arr = { 9 , 3 , 4 , 2 };
int n = arr.length;
System.out.println(findSumofProduct(arr, n));
}
}
|
Python 3
def findSumofProduct(arr, n):
suffix_sum = arr[n - 1 ]
res = 0
for i in range (n - 2 , - 1 , - 1 ):
res + = (suffix_sum * arr[i])
suffix_sum + = arr[i]
return res;
arr = [ 9 , 3 , 4 , 2 ];
n = len (arr);
print (findSumofProduct(arr, n))
|
C#
using System;
class GFG {
static int findSumofProduct( int [] arr,
int n)
{
int suffix_sum = arr[n - 1];
int res = 0;
for ( int i = n - 2; i >= 0; i--)
{
res += (suffix_sum * arr[i]);
suffix_sum += arr[i];
}
return res;
}
static public void Main()
{
int [] arr = {9, 3, 4, 2};
int n = arr.Length;
Console.WriteLine(findSumofProduct(arr, n));
}
}
|
PHP
<?php
function findSumofProduct( $arr ,
$n )
{
$suffix_sum = $arr [ $n - 1];
$res = 0;
for ( $i = $n - 2; $i >= 0; $i --)
{
$res += ( $suffix_sum * $arr [ $i ]);
$suffix_sum += $arr [ $i ];
}
return $res ;
}
$arr = array (9, 3, 4, 2);
$n = count ( $arr );
echo findSumofProduct( $arr , $n )
?>
|
Javascript
<script>
function findSumofProduct(arr, n)
{
let suffix_sum = arr[n - 1];
let res = 0;
for (let i = n - 2; i >= 0; i--)
{
res += (suffix_sum * arr[i]);
suffix_sum += arr[i];
}
return res;
}
let arr = [9, 3, 4, 2];
let n = arr.length;
document.write(findSumofProduct(arr, n));
</script>
|
Time complexity: O(n) where n is the size of the given array
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Oct, 2022
Like Article
Save Article