Sum of product of each element with each element after it

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
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find sum of 
// product of each element
// with each element after it
#include <bits/stdc++.h>
using namespace std;
  
// Return sum of product 
// of each element with 
// each element after it
int findSumofProduct(int arr[], int n)
{
    int sum[n];
    sum[n - 1] = arr[n - 1];
  
    // finding suffix sum
    for (int i = n - 2; i >= 0; i--) 
        sum[i] = sum[i + 1] + arr[i];
      
    // finding the sum of product of 
    // each element with suffix sum.
    int res = 0;
    for (int i = 0; i < n - 1; i++) 
        res += (sum[i + 1] * arr[i]); 
  
    return res;
}
  
// Driver Code
int main()
{
    int arr[] = {9, 3, 4, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findSumofProduct(arr, n)
         << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find sum of product
// of each element with each element
// after it
class GFG {
      
    // Return sum of product of each
    // element with each element
    // after it
    static int findSumofProduct(int arr[],
                                    int n)
    {
        int sum[] = new int[n];
        sum[n - 1] = arr[n - 1];
      
        // finding suffix sum
        for (int i = n - 2; i >= 0; i--) 
            sum[i] = sum[i + 1] + arr[i];
          
        // finding the sum of product of
        // each element with suffix sum.
        int res = 0;
        for (int i = 0; i < n - 1; i++) 
            res += (sum[i + 1] * arr[i]); 
      
        return res;
    }
      
    // Driver Program
    public static void main(String[] args)
    {
          
        int arr[] = { 9, 3, 4, 2 };
        int n = arr.length;
          
        System.out.println(
                findSumofProduct(arr, n));
    }
}
  
// This code is contributed by
// Smitha Dinesh Semwal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find sum of 
# product of each element with 
# each element after it 
  
# Return sum of product of each 
# element with each element after it 
def findSumofProduct(arr, n):
    sum = [None for _ in range(n)]
    sum[n-1] = arr[n - 1]
  
    # finding suffix sum 
    for i in range(n - 2, -1, -1):
        sum[i] = sum[i + 1] + arr[i]
  
    # finding the sum of product of 
    # each element with suffix sum. 
    res = 0
    for i in range(n - 1):
        res += sum[i + 1] * arr[i]
    return res
  
# Driver Code
arr = [9, 3, 4, 2]
n = len(arr)
print(findSumofProduct(arr, n))
  
# This code is contributed
# by SamyuktaSHegde

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find sum of product
// of each element with each element
// after it
using System;
  
class GFG {
      
    // Return sum of product of each
    // element with each element
    // after it
    static int findSumofProduct(int[] arr,
                                    int n)
    {
        int[] sum = new int[n];
        sum[n - 1] = arr[n - 1];
      
        // finding suffix sum
        for (int i = n - 2; i >= 0; i--) 
            sum[i] = sum[i + 1] + arr[i];
          
        // finding the sum of product of
        // each element with suffix sum.
        int res = 0;
        for (int i = 0; i < n - 1; i++) 
            res += (sum[i + 1] * arr[i]); 
      
        return res;
    }
      
    // Driver code
    static public void Main ()
    {
        int[] arr = { 9, 3, 4, 2 };
        int n = arr.Length;
          
        Console.WriteLine(findSumofProduct(arr, n));
    }
}
  
// This code is contributed by Ajit.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find sum of 
// product of each element
// with each element after it
  
// Return sum of product 
// of each element with 
// each element after it
function findSumofProduct($arr, $n)
{
      
    $sum = array();
    $sum[$n - 1] = $arr[$n - 1];
  
    // finding suffix sum
    for ( $i = $n - 2; $i >= 0; $i--) 
        $sum[$i] = $sum[$i + 1] + $arr[$i];
      
    // finding the sum of product of 
    // each element with suffix sum.
    $res = 0;
    for ( $i = 0; $i < $n - 1; $i++) 
        $res += ($sum[$i + 1] * $arr[$i]); 
  
    return $res;
}
  
    // Driver Code
    $arr = array(9, 3, 4, 2);
    $n = count($arr);
    echo findSumofProduct($arr, $n);
      
// This code is contributed by anuJ_67.
?>

chevron_right


Output:

107

Below is Optimized Code of finding sum of product of each element with each element after it:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find sum of
// product of each element 
// with each element after it
#include <bits/stdc++.h>
using namespace std;
  
// Return sum of product 
// of each element with 
// each element after it
int findSumofProduct(int arr[], 
                     int n)
{
    int suffix_sum = arr[n - 1];
  
    // Finding product of array 
    // elements and suffix sum.
    int res = 0;
    for (int i = n - 2; i >= 0; i--) 
    {
  
        res += (suffix_sum * arr[i]);
  
        // finding suffix sum
        suffix_sum += arr[i];
    }
  
    return res;
}
  
// Driver Code
int main()
{
    int arr[] = {9, 3, 4, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findSumofProduct(arr, n) 
         << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find sum 
// of product of each 
// element with each
// element after it
import java .io.*;
class GFG {
      
    // Return sum of product of
    // each element with each
    // element after it
    static int findSumofProduct(int[] arr,
                                int n)
    {
        int suffix_sum = arr[n - 1];
      
        // Finding product of array 
        // elements and suffix sum.
        int res = 0;
        for (int i = n - 2; i >= 0; i--)
        {
            res += (suffix_sum * arr[i]);
      
            // finding suffix sum
            suffix_sum += arr[i];
        }
      
        return res;
    }
  
    // Driver code
    static public void main(String[] args)
    {
        int[] arr = {9, 3, 4, 2};
        int n = arr.length;
        System.out.println(findSumofProduct(arr, n));
    }
}
  
// This code is contributed by anuj_67.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program to find sum of
# product of each element 
# with each element after it
  
# Return sum of product 
# of each element with 
# each element after it
def findSumofProduct(arr, n):
    suffix_sum = arr[n - 1]
  
    # Finding product of array 
    # elements and suffix sum.
    res = 0
    for i in range(n - 2, -1, -1):
  
        res += (suffix_sum * arr[i])
  
        # finding suffix sum
        suffix_sum += arr[i]
  
    return res;
  
# Driver Code
arr = [9, 3, 4, 2];
n = len(arr);
print(findSumofProduct(arr, n))
  
# This code is contributed 
# by Akanksha Rai

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find sum 
// of product of each 
// element with each
// element after it
using System;
  
class GFG {
      
    // Return sum of product of
    // each element with each
    // element after it
    static int findSumofProduct(int[] arr,
                                int n)
    {
        int suffix_sum = arr[n - 1];
      
        // Finding product of array 
        // elements and suffix sum.
        int res = 0;
        for (int i = n - 2; i >= 0; i--)
        {
            res += (suffix_sum * arr[i]);
      
            // finding suffix sum
            suffix_sum += arr[i];
        }
      
        return res;
    }
  
    // Driver code
    static public void Main()
    {
        int[] arr = {9, 3, 4, 2};
        int n = arr.Length;
        Console.WriteLine(findSumofProduct(arr, n));
    }
}
  
// This code is contributed by Ajit.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find sum of
// product of each element 
// with each element after it
  
// Return sum of product 
// of each element with 
// each element after it
function findSumofProduct($arr
                          $n)
{
    $suffix_sum = $arr[$n - 1];
  
    // Finding product of array 
    // elements and suffix sum.
    $res = 0;
    for ( $i = $n - 2; $i >= 0; $i--) 
    {
  
        $res += ($suffix_sum * $arr[$i]);
  
        // finding suffix sum
        $suffix_sum += $arr[$i];
    }
  
    return $res;
}
  
    // Driver Code
    $arr = array(9, 3, 4, 2);
    $n = count($arr);
    echo findSumofProduct($arr, $n
  
  
// This code is contributed by anuJ_67.
?>

chevron_right


Output:

107


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.