📌  相关文章
📜  每个元素与其后的每个元素的乘积之和

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

每个元素与其后的每个元素的乘积之和

给定一个包含 n 个整数的数组 arr[]。任务是找到数组中每个元素与其后的每个元素的乘积之和。换句话说,求每个 arr[i] 与每个 arr[j] 的乘积之和,使得 j > i。
例子 :

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

观察发现 (p*a + p*b + .....+ p*y + p*z) 等于 p*(a + b ..... y + z)。
所以对于每个 i, 0 ≤ i ≤ n – 2 我们必须计算 arr[i] * (arr[j] + arr[j+1] + ..... + arr[n – 2] + arr[n – 1 ]),其中 j = i + 1 并找到这些的总和。我们可以通过预先计算每个 'i' 的后缀和来降低找到 (arr[j] + arr[j+1] + ..... + arr[n – 2] + arr[n – 1]) 的复杂度大批。
让 sum[] 存储给定数组的后缀和,即 sum[i] 有 arr[i] + arr[i+1] + ..... + arr[n – 2] + arr[n – 1]。
因此,在预计算 sum[] 之后,我们需要找到 arr[i]*sum[i + 1] 的和,其中 0 ≤ i ≤ n – 2。
下面是这种方法的实现:

C++
// C++ Program to find sum of
// product of each element
// with each element after it
#include 
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;
}


Java
// 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


Python3
# 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


C#
// 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.


PHP
= 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.
?>


Javascript


C++
// C++ Program to find sum of
// product of each element
// with each element after it
#include 
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;
}


Java
// 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.


Python 3
# 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


C#
// 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.


PHP
= 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.
?>


Javascript


输出:
107

以下是查找每个元素与之后的每个元素的乘积之和的优化代码:

C++

// C++ Program to find sum of
// product of each element
// with each element after it
#include 
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;
}

Java

// 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.

Python3

# 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

C#

// 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.

PHP

= 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.
?>

Javascript


输出:
107