Find the sum of all possible pairs in an array of N elements

19 Mar, 2022
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.
(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 valid pairs are (1, 1), (1, 2), (2, 1) and (2, 2). 1 + 1 + 1 + 2 + 2 + 1 + 2 + 2 = 12
Input: arr[] = {1, 2, 3, 1, 4} Output: 110  
Naive approach: Find all the possible pairs and calculate the sum of the elements of each pair.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the sum of the elements// of all possible pairs from the arrayint sumPairs(int arr[], int n){     // To store the required sum    int sum = 0;     // Nested loop for all possible pairs    for (int i = 0; i < n; i++) {        for (int j = 0; j < n; j++) {             // Add the sum of the elements            // of the current pair            sum += (arr[i] + arr[j]);        }    }    return sum;} // Driver codeint main(){    int arr[] = { 1, 2, 3 };    int n = sizeof(arr) / sizeof(arr[0]);     cout << sumPairs(arr, n);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{     // Function to return the sum of the elements    // of all possible pairs from the array    static int sumPairs(int arr[], int n)    {         // To store the required sum        int sum = 0;         // Nested loop for all possible pairs        for (int i = 0; i < n; i++)        {            for (int j = 0; j < n; j++)            {                 // Add the sum of the elements                // of the current pair                sum += (arr[i] + arr[j]);            }        }        return sum;    }     // Driver code    public static void main(String[] args)    {        int arr[] = {1, 2, 3};        int n = arr.length;         System.out.println(sumPairs(arr, n));    }} // This code is contributed by PrinciRaj1992
# Python3 implementation of the approach # Function to return the summ of the elements# of all possible pairs from the arraydef summPairs(arr, n):     # To store the required summ    summ = 0     # Nested loop for all possible pairs    for i in range(n):        for j in range(n):             # Add the summ of the elements            # of the current pair            summ += (arr[i] + arr[j])     return summ # Driver codearr = [1, 2, 3]n = len(arr) print(summPairs(arr, n)) # This code is contributed by Mohit Kumar
// C# implementation of the approachusing System; class GFG{     // Function to return the sum of the elements    // of all possible pairs from the array    static int sumPairs(int []arr, int n)    {         // To store the required sum        int sum = 0;         // Nested loop for all possible pairs        for (int i = 0; i < n; i++)        {            for (int j = 0; j < n; j++)            {                 // Add the sum of the elements                // of the current pair                sum += (arr[i] + arr[j]);            }        }        return sum;    }     // Driver code    public static void Main(String[] args)    {        int []arr = {1, 2, 3};        int n = arr.Length;         Console.WriteLine(sumPairs(arr, n));    }}     // This code is contributed by PrinciRaj1992
<script> // Javascript implementation of the approach // Function to return the sum of the elements// of all possible pairs from the arrayfunction sumPairs(arr, n){         // To store the required sum    var sum = 0;    var i, j;         // Nested loop for all possible pairs    for(i = 0; i < n; i++)    {        for(j = 0; j < n; j++)        {                         // Add the sum of the elements            // of the current pair            sum += (arr[i] + arr[j]);        }    }    return sum;} // Driver codevar arr = [ 1, 2, 3 ];var n = arr.length; document.write(sumPairs(arr, n)); // This code is contributed by ipg2016107 </script>
36
 
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient approach: It can be observed that each element appears exactly (2 * N) times as one of the elements of the pair (x, y). Exactly N times as x and exactly N times as y.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the sum of the elements// of all possible pairs from the arrayint sumPairs(int arr[], int n){     // To store the required sum    int sum = 0;     // For every element of the array    for (int i = 0; i < n; i++) {         // It appears (2 * n) times        sum = sum + (arr[i] * (2 * n));    }     return sum;} // Driver codeint main(){    int arr[] = { 1, 2, 3 };    int n = sizeof(arr) / sizeof(arr[0]);     cout << sumPairs(arr, n);     return 0;}
// Java implementation of the approachimport java.util.*;     class GFG{ // Function to return the sum of the elements// of all possible pairs from the arraystatic int sumPairs(int arr[], int n){     // To store the required sum    int sum = 0;     // For every element of the array    for (int i = 0; i < n; i++)    {         // It appears (2 * n) times        sum = sum + (arr[i] * (2 * n));    }     return sum;} // Driver codestatic public void main(String []arg){    int arr[] = { 1, 2, 3 };    int n = arr.length;     System.out.println(sumPairs(arr, n));}} // This code is contributed by 29AjayKumar
# Python3 implementation of the approach # Function to return the sum of the elements# of all possible pairs from the arraydef sumPairs(arr, n) :     # To store the required sum    sum = 0;     # For every element of the array    for i in range(n) :         # It appears (2 * n) times        sum = sum + (arr[i] * (2 * n));     return sum; # Driver codeif __name__ == "__main__" :     arr = [ 1, 2, 3 ];    n = len(arr);     print(sumPairs(arr, n)); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System;         class GFG{ // Function to return the sum of the elements// of all possible pairs from the arraystatic int sumPairs(int []arr, int n){     // To store the required sum    int sum = 0;     // For every element of the array    for (int i = 0; i < n; i++)    {         // It appears (2 * n) times        sum = sum + (arr[i] * (2 * n));    }     return sum;} // Driver codestatic public void Main(String []arg){    int []arr = { 1, 2, 3 };    int n = arr.Length;     Console.WriteLine(sumPairs(arr, n));}} // This code contributed by Rajput-Ji
<script> // Javascript implementation of the approach // Function to return the sum of the elements// of all possible pairs from the arrayfunction sumPairs(arr, n){     // To store the required sum    let sum = 0;     // For every element of the array    for (let i = 0; i < n; i++) {         // It appears (2 * n) times        sum = sum + (arr[i] * (2 * n));    }     return sum;} // Driver code    let arr = [ 1, 2, 3 ];    let n = arr.length;     document.write(sumPairs(arr, n)); </script>
36
 
Time Complexity: O(N) Auxiliary Space: O(1)
mohit kumar 29
ankthon
princiraj1992
29AjayKumar
Rajput-Ji
ipg2016107
rishavmahato348
subhamkumarm348
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multidimensional Arrays in Java
Maximum and minimum of an array using minimum number of comparisons
Python | Using 2D arrays/lists the right way
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
Find a triplet that sum to a given value
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
Operators in C / C++