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: 36
Explanation:All possible pairs are {(1, 1), {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}}. Therefore, the sum of product of all pairs is 36.

Input : arr[]={3, 4, 1, 2, 5}
Output: 225 

Naive Approach: The simplest approach is to iterate through all possible pairs from the given array calculate the sum of product of all pair-products. 

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the following observations:



arr[]={a1, a2, a3, a4 ….., an-1, an}
Now, sum of product of all  possible pairs is =
{
(a1 * a1) + (a1 * a2) + (a1 * a3) + …..+ (a1 * an-1) + (a1, an) +
(a2 * a1) + (a2 * a2) + (a2 * a3) + ….. + (a2 * an-1) + (a2 * an) +
(a3 * a1) + (a3 * a2) + (a3 * a3) + ….. + (a3 * an-1) + (a3, an) +
…………………………………………………………………………..
(an, * a1) + (an * a2), +(an * a3) + ….. + (an * an-1) + (an, an)
}

={
(a1 + a2+  a3 + …..+ an-1 +  an * (a1 + a2+  a3 + …..+ an-1 +  an
}
=(a1 + a2+  a3 + …..+ an-1 +  an )2

Follow the steps below to solve the problem: 

  1. Initialize the variable, res=0 to store the sum of array elements
  2. Traverse the array, arr[] and add each element of the array to res.
  3. Finally, print the square of the res as the required answer.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the
// sum of all pair-products
int sumOfProd(int arr[], int N)
{
    // Stores sum of array
    int sum = 0;
 
    for (int i = 0; i < N; i++) {
 
        // Update sum of the array
        sum += arr[i];
    }
 
    return sum * sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 1, 5, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << sumOfProd(arr, N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to calculate the
// sum of all pair-products
static int sumOfProd(int arr[], int N)
{
     
    // Stores sum of array
    int sum = 0;
 
    for(int i = 0; i < N; i++)
    {
         
        // Update sum of the array
        sum += arr[i];
    }
    return sum * sum;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 3, 1, 5, 4 };
    int N = arr.length;
     
    System.out.print(sumOfProd(arr, N));
}
}
 
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach 
 
# Function to calculate the
# sum of all pair-products
def sumOfProd(arr, N):
     
    # Stores sum of array
    sum = 0
 
    for i in range(N):
         
        # Update sum of the array
        sum += arr[i]
 
    return sum * sum
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 2, 3, 1, 5, 4 ]
    N = len(arr)
     
    print(sumOfProd(arr, N))
 
# This code is contributed by SURENDRA_GANGWAR

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement 
// the above approach  
using System;
 
class GFG{
     
// Function to calculate the 
// sum of all pair-products 
static int sumOfProd(int[] arr, int N) 
     
    // Stores sum of array 
    int sum = 0; 
   
    for(int i = 0; i < N; i++)
    {
         
        // Update sum of the array 
        sum += arr[i]; 
    
    return sum * sum; 
}  
 
// Driver code
static void Main()
{
    int[] arr = { 2, 3, 1, 5, 4 }; 
    int N = arr.Length; 
     
    Console.WriteLine(sumOfProd(arr, N));
}
}
 
// This code is contributed by divyeshrabadiya07

chevron_right


Output:

225

Time Complexity: O(N)

Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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.