Mean of array generated by products of all pairs of the given array

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the mean of pair
// product array of arr[]
float pairProductMean(int arr[], int N)
{
    // Store product of pairs
    vector<int> pairArray;
  
    // Generate all unordered pairs
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
  
            int pairProduct
                = arr[i] * arr[j];
  
            // Store product of pairs
            pairArray.push_back(pairProduct);
        }
    }
  
    // Size of pairArray
    int length = pairArray.size();
  
    // Store sum of pairArray
    float sum = 0;
    for (int i = 0; i < length; i++)
        sum += pairArray[i];
  
    // Stores the mean of pairArray[]
    float mean;
  
    // Find mean of pairArray[]
    if (length != 0)
        mean = sum / length;
    else
        mean = 0;
  
    // Return the resultant mean
    return mean;
}
  
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 4, 8 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    cout << fixed << setprecision(2)
         << pairProductMean(arr, N);
  
    return 0;
}

chevron_right


Output:

11.67

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the mean of pair
// product array of arr[]
float pairProductMean(int arr[], int N)
{
    // Initializing suffix sum array
    int suffixSumArray[N];
    suffixSumArray[N - 1] = arr[N - 1];
  
    // Build suffix sum array
    for (int i = N - 2; i >= 0; i--) {
        suffixSumArray[i]
            = suffixSumArray[i + 1]
              + arr[i];
    }
  
    // Size of pairProductArray
    int length = (N * (N - 1)) / 2;
  
    // Stores sum of pairProductArray
    float res = 0;
  
    for (int i = 0; i < N - 1; i++) {
        res += arr[i]
               * suffixSumArray[i + 1];
    }
  
    // Store the mean
    float mean;
  
    // Find mean of pairProductArray
    if (length != 0)
        mean = res / length;
    else
        mean = 0;
  
    // Return the resultant mean
    return mean;
}
  
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 4, 8 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    cout << fixed << setprecision(2)
         << pairProductMean(arr, N);
  
    return 0;
}

chevron_right


Output:

11.67

Time Complexity: O(N)
Auxiliary Space: 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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.