Related Articles

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

• Difficulty Level : Easy
• Last Updated : 03 May, 2021

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,

1. (arr[i], arr[i]) is also considered as a valid pair.
2. (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++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to return the sum of the elements``// of all possible pairs from the array``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``int` `main()``{``    ``int` `arr[] = { 1, 2, 3 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);` `    ``cout << sumPairs(arr, n);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `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

 `# Python3 implementation of the approach` `# Function to return the summ of the elements``# of all possible pairs from the array``def` `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 code``arr ``=` `[``1``, ``2``, ``3``]``n ``=` `len``(arr)` `print``(summPairs(arr, n))` `# This code is contributed by Mohit Kumar`

## C#

 `// C# implementation of the approach``using` `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`

## Javascript

 ``
Output:
`36`

Time Complexity: O(N2)

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

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to return the sum of the elements``// of all possible pairs from the array``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 code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);` `    ``cout << sumPairs(arr, n);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `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``;` `    ``// 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 code``static` `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

 `# Python3 implementation of the approach` `# Function to return the sum of the elements``# of all possible pairs from the array``def` `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 code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``1``, ``2``, ``3` `];``    ``n ``=` `len``(arr);` `    ``print``(sumPairs(arr, n));` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach``using` `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;` `    ``// 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 code``static` `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`

## Javascript

 ``
Output:
`36`

Time Complexity: O(N)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up