- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of Pairwise Products
The pairwise product of a set X = {a, b, c} can be defined as the sum of the product of all possible set pairs. The pairs of the set are, Y = {a * a, a * b, a *c, b * b, b * c, c * c}, where the product is commutative. Thus, the pairwise product of set X is the summation of elements of set Y i.e. aa + ab + ac + bb + bc + cc.
In mathematical terms, the sum of possible pair products can be depicted as,
$$\mathrm{\displaystyle\sum\limits_{i=1,j=i}^{i\leq n,j\leq n}\:(i,j)=i\times j}$$
Problem Statement
Given a number n. Find the sum of pairwise products over the range (1, n), both n and 1 inclusive.
Sample Example 1
Input: n = 4
Output: 65
Explanation
i ranges from 1 to 4 and j ranges from i to 4.
1*1 + 1*2 + 1*3 + 1*4 + 2*2 + 2*3 + 2*4 + 3*3 + 3*4 + 4*4 = 1 + 2 + 3 + 4 + 4 + 6 + 8 + 9 + 12 + 16 = 65
Sample Example 2
Input: n = 10
Output: 1705
Explanation
i ranges from 1 to 10 and j ranges from i to 10.
1*1 + 1*2 + … + 1*10 + 2*2 + 2*3 + … + 2*10 + 3*3 + 3*4 + … + 3*10 + 4*4 + 4*5 + … 4*10 + 5*5 + 5*6 + … + 5*10 + 6*6 + 6*7 + … 6*10 + 7*7 + 7*8 + … 7*10 + 8*8 + 8*9 + 8*10 + 9*9 + 9*10 + 10*10 = 1705
Approach 1: Brute Force Approach
The brute force solution to the problem is to iterate through all the possible pairs in the range using two for loops where the first loop iterates over 1 to n and second loop iterate over the first number to n.
Pseudocode
procedure pairwiseProduct (n) sum = 0 for i = 1 to n for j = i to n sum = sum + (i * j) end procedure
Example: C++ Implementation
In the following program, we find all the possible pairs and then find the sum of products.
#include <bits/stdc++.h> using namespace std; // Function to find pairwise product over the range 1 to n, 1 and n inclusive unsigned long long pairwiseProduct(unsigned int n){ unsigned long long sum = 0; // First number: 1 <= i <= n for (unsigned int i = 1; i <= n; i++){ // Second number: i <= j <= n for (unsigned int j = i; j <= n; j++){ sum += i * j; } } return sum; } int main(){ unsigned long long n = 9; cout << "Pairwise Product = " << pairwiseProduct(n); return 0; }
Output
Pairwise Product = 1155
Time Complexity − O(n^2)
Space Complexity − O(1)
Approach 2
Taking an example of n = 4,
Sum = 1*1 + 1*2 + 1*3 + 1*4 + 2*2 + 2*3 + 2*4 + 3*3 + 3*4 + 4*4
On simplifying the above,
Sum = 1*1 + (1+2)*2 + (1+2+3)*3 + (1+2+3+4)*4
Taking prefix_sum[1] = 1,
prefix_sum[2] = 1+2,
prefix_sum[3] = 1+2+3,
prefix_sum[2] = 1+2,
Pseudocode
procedure pairwiseProduct (n) sum = 0 prefixSum = 0 for i = 1 to n prefixSum = prefixSum + 1 sum = sum + i * prefixSum end procedure
Example: C++ Implementation
In the following program, we find the sum i.e. prefix sum at each iteration and multiply by the no of iterations and add to final sum at each step.
#include <bits/stdc++.h> using namespace std; // Function to find pairwise product over the range 1 to n, 1 and n inclusive unsigned long long pairwiseProduct(unsigned int n){ unsigned long long sum = 0; unsigned long long prefixSum = 0; for (unsigned int i = 1; i <= n; i++){ prefixSum += i; sum += i * prefixSum; } return sum; } int main(){ unsigned long long n = 9; cout << "Pairwise Product = " << pairwiseProduct(n); return 0; }
Output
Pairwise Product = 1155
Conclusion
In conclusion, for finding the sum of the pairwise product of numbers in the range 1 to n, both 1 and n inclusive, we can follow any of the above 2 approaches where the first one is the bute force approach with time complexity O(n^2) and the second approach is the optimized approach using prefix sum to calculate the sum of the pairwise product having a time complexity of O(n).
To Continue Learning Please Login
Login with Google