Find the sum of all possible pairs in an array of N elements
Last Updated :
07 Jan, 2024
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.
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 <bits/stdc++.h>
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[0]);
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
<script>
// Javascript implementation of the approach
// Function to return the sum of the elements
// of all possible pairs from the array
function 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 code
var arr = [ 1, 2, 3 ];
var n = arr.length;
document.write(sumPairs(arr, n));
// This code is contributed by ipg2016107
</script>
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++
// 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 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[0]);
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
<script>
// Javascript implementation of the approach
// Function to return the sum of the elements
// of all possible pairs from the array
function 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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Sum of Bitwise AND of all pairs possible from two arrays Given two arrays A[] and B[] of size N and M respectively, the task is to find the sum of Bitwise AND of all possible unordered pairs (A[i], B[j]) from the two arrays. Examples: Input: A[] = {1, 2} , B[] = {3, 4} Output: 3 Explanation: Bitwise AND of all possible pairs are 1 & 3 = 1 1 & 4 =
5 min read
XOR of Sum of every possible pair of an array Given an array A of size n. the task is to generate a new sequence B with size N^2 having elements sum of every pair of array A and find the xor value of the sum of all the pairs formed. Note: Here (A[i], A[i]), (A[i], A[j]), (A[j], A[i]) all are considered as different pairs. Examples: Input: arr[]
5 min read
Sum of Bitwise And of all pairs in a given array Given an array "arr[0..n-1]" of integers, calculate sum of "arr[i] & arr[j]" for all the pairs in the given where i < j. Here & is bitwise AND operator. Expected time complexity is O(n). Examples : Input: arr[] = {5, 10, 15} Output: 15 Required Value = (5 & 10) + (5 & 15) + (10
13 min read
Find all Pairs possible from the given Array Given an array arr[] of N integers, the task is to find all the pairs possible from the given array. Note: (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: (1, 1), (1, 2), (2, 1), (
4 min read
Sum of XOR of all pairs in an array Given an array of n integers, find the sum of xor of all pairs of numbers in the array. Examples : Input : arr[] = {7, 3, 5}Output : 127 ^ 3 = 43 ^ 5 = 67 ^ 5 = 2Sum = 4 + 6 + 2 = 12Input : arr[] = {5, 9, 7, 6}Output : 475 ^ 9 = 129 ^ 7 = 147 ^ 6 = 15 ^ 7 = 25 ^ 6 = 39 ^ 6 = 15Sum = 12 + 14 + 1 + 2
11 min read