Given an array arr[] consisting of N integers, the task is to find the mean of the array formed by the products of unordered pairs of the given array.
Examples:
Input: arr[] = {2, 5, 7}
Output: 19.67
Explanation:
Product of unordered pairs of array arr[] are 2 * 5 = 10, 2 * 7 = 14 and 5 * 7 = 35.
Therefore, the resultant array of product of pairs is {10, 14, 35}.
Mean of the array of product of pairs is 59/3 = 19.67
Input: arr[] = {1, 2, 4, 8}
Output: 11.67
Explanation:
Product of unordered pairs of array arr[] are 1 * 2 = 2, 1 * 4 = 4, 1 * 8 = 8, 2 * 4 = 8, 2 * 8 = 16, 4 * 8 = 32.
Therefore, the resultant array of product of pairs is {2, 4, 8, 8, 16, 32}.
Mean of the array of product of pairs is 70/6 i.e., 11.67
Naive Approach: The simplest approach to solve the problem is to generate all possible pairs of array pairProductArray[] i.e., array formed by the product of unordered pairs of the array arr[]. Then, find the mean of the pairProductArray[]. Follow the steps below to solve the problem:
- Generate all possible pairs of the array arr[] and store their products in pairProductArray[].
- Initialize a variable sum to store the sum of the elements of pairProductArray[].
- Divide the variable sum with the size of pairProductArray[] to get the required mean.
- Finally, print the value of the sum as the resultant mean.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float pairProductMean( int arr[], int N)
{
vector< int > pairArray;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int pairProduct
= arr[i] * arr[j];
pairArray.push_back(pairProduct);
}
}
int length = pairArray.size();
float sum = 0;
for ( int i = 0; i < length; i++)
sum += pairArray[i];
float mean;
if (length != 0)
mean = sum / length;
else
mean = 0;
return mean;
}
int main()
{
int arr[] = { 1, 2, 4, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << fixed << setprecision(2)
<< pairProductMean(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static double pairProductMean( int arr[],
int N)
{
Vector<Integer> pairArray =
new Vector<>();
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
int pairProduct = arr[i] *
arr[j];
pairArray.add(pairProduct);
}
}
int length = pairArray.size();
float sum = 0 ;
for ( int i = 0 ; i < length; i++)
sum += pairArray.get(i);
float mean;
if (length != 0 )
mean = sum / length;
else
mean = 0 ;
return mean;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 , 8 };
int N = arr.length;
System.out.format( "%.2f" ,
pairProductMean(arr, N));
}
}
|
Python3
def pairProductMean(arr, N):
pairArray = [];
for i in range (N):
for j in range (i + 1 , N):
pairProduct = arr[i] * arr[j];
pairArray.append(pairProduct);
length = len (pairArray);
sum = 0 ;
for i in range (length):
sum + = pairArray[i];
mean = 0 ;
if (length ! = 0 ):
mean = sum / length;
else :
mean = 0 ;
return mean;
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 8 ];
N = len (arr);
print ( "{0:.2f}" . format (
pairProductMean(arr, N)))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static double pairProductMean( int []arr,
int N)
{
List< int > pairArray =
new List< int >();
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
int pairProduct = arr[i] *
arr[j];
pairArray.Add(pairProduct);
}
}
int length = pairArray.Count;
float sum = 0;
for ( int i = 0; i < length; i++)
sum += pairArray[i];
float mean;
if (length != 0)
mean = sum / length;
else
mean = 0;
return mean;
}
public static void Main(String[] args)
{
int []arr = {1, 2, 4, 8};
int N = arr.Length;
Console.WriteLine( "{0:F2}" ,
pairProductMean(arr,
N));
}
}
|
Javascript
<script>
function pairProductMean(arr , N)
{
var pairArray = [];
for (i = 0; i < N; i++) {
for (j = i + 1; j < N; j++) {
var pairProduct = arr[i] * arr[j];
pairArray.push(pairProduct);
}
}
var length = pairArray.length;
var sum = 0;
for (i = 0; i < length; i++)
sum += pairArray[i];
var mean;
if (length != 0)
mean = sum / length;
else
mean = 0;
return mean;
}
var arr = [ 1, 2, 4, 8 ];
var N = arr.length;
document.write(pairProductMean(arr, N).toFixed(2));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Efficient Approach: The idea is to use the fact that every element arr[i] is multiplied with every element arr[j] which is on the right side of the element arr[i], more formally element at index i is multiplied to all the elements positioned at index j such that j > i. Follow the steps below to solve the problem:
- Create a suffix sum array suffixSumArray[] for the given array arr[].
- Initialize variable res to store the sum of product pairs of array arr[].
- Iterate array arr[] and for each position i incrementing res with arr[i]*suffixSumArray[i+1].
- Divide the variable res with N*(N – 1)/ 2 which is the number of possible products.
- Finally, print the value of res as the resultant mean.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float pairProductMean( int arr[], int N)
{
int suffixSumArray[N];
suffixSumArray[N - 1] = arr[N - 1];
for ( int i = N - 2; i >= 0; i--) {
suffixSumArray[i]
= suffixSumArray[i + 1]
+ arr[i];
}
int length = (N * (N - 1)) / 2;
float res = 0;
for ( int i = 0; i < N - 1; i++) {
res += arr[i]
* suffixSumArray[i + 1];
}
float mean;
if (length != 0)
mean = res / length;
else
mean = 0;
return mean;
}
int main()
{
int arr[] = { 1, 2, 4, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << fixed << setprecision(2)
<< pairProductMean(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static float pairProductMean( int arr[], int N)
{
int suffixSumArray[] = new int [N];
suffixSumArray[N - 1 ] = arr[N - 1 ];
for ( int i = N - 2 ; i >= 0 ; i--)
{
suffixSumArray[i] = suffixSumArray[i + 1 ] +
arr[i];
}
int length = (N * (N - 1 )) / 2 ;
float res = 0 ;
for ( int i = 0 ; i < N - 1 ; i++)
{
res += arr[i] *
suffixSumArray[i + 1 ];
}
float mean;
if (length != 0 )
mean = res / length;
else
mean = 0 ;
return mean;
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 4 , 8 };
int N = arr.length;
System.out.format( "%.2f" ,
pairProductMean(arr, N));
}
}
|
Python3
def pairProductMean(arr, N):
suffixSumArray = [ 0 ] * N
suffixSumArray[N - 1 ] = arr[N - 1 ]
for i in range (N - 2 , - 1 , - 1 ):
suffixSumArray[i] = suffixSumArray[i + 1 ] + arr[i]
length = (N * (N - 1 )) / / 2
res = 0
for i in range (N - 1 ):
res + = arr[i] * suffixSumArray[i + 1 ]
mean = 0
if (length ! = 0 ):
mean = res / length
else :
mean = 0
return mean
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 8 ]
N = len (arr)
print ( round (pairProductMean(arr, N), 2 ))
|
C#
using System;
class GFG{
static double pairProductMean( int [] arr, int N)
{
int [] suffixSumArray = new int [N];
suffixSumArray[N - 1] = arr[N - 1];
for ( int i = N - 2; i >= 0; i--)
{
suffixSumArray[i] = suffixSumArray[i + 1] +
arr[i];
}
int length = (N * (N - 1)) / 2;
double res = 0;
for ( int i = 0; i < N - 1; i++)
{
res += arr[i] *
suffixSumArray[i + 1];
}
double mean;
if (length != 0)
mean = res / length;
else
mean = 0;
return mean;
}
public static void Main()
{
int [] arr = { 1, 2, 4, 8 };
int N = arr.Length;
Console.WriteLine( string .Format( "{0:0.00}" ,
pairProductMean(arr, N)));
}
}
|
Javascript
<script>
function pairProductMean(arr, N)
{
var suffixSumArray = Array(N);
suffixSumArray[N - 1] = arr[N - 1];
for ( var i = N - 2; i >= 0; i--) {
suffixSumArray[i]
= suffixSumArray[i + 1]
+ arr[i];
}
var length = (N * (N - 1)) / 2;
var res = 0;
for ( var i = 0; i < N - 1; i++) {
res += arr[i]
* suffixSumArray[i + 1];
}
var mean;
if (length != 0)
mean = res / length;
else
mean = 0;
return mean;
}
var arr = [ 1, 2, 4, 8 ];
var N = arr.length;
document.write( pairProductMean(arr, N).toFixed(2));
</script>
|
Complexity analysis:
The time complexity of the given program is O(N), where N is the size of the input array. This is because the program iterates over the input array twice, once to build the suffix sum array, and once to calculate the sum of the pair product array.
The space complexity of the program is O(N), as it creates an array of size N to store the suffix sum array.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Apr, 2023
Like Article
Save Article