📜  由给定数组的所有对的乘积生成的数组的平均值

📅  最后修改于: 2021-09-07 04:29:47             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是找到由给定数组的无序对的乘积形成的数组的均值。

例子:

朴素的方法:解决问题的最简单的方法是生成数组pairProductArray[] 的所有可能对,即由数组arr[]的无序对的乘积形成的数组。然后,找到pairProductArray[]的平均值。请按照以下步骤解决问题:

  • 生成数组arr[] 的所有可能对并将它们的产品存储在pairProductArray[] 中
  • 初始化一个变量sum以存储pairProductArray[]的元素的总和。
  • 将变量sumpairProductArray[]大小相除以获得所需的平均值。
  • 最后,打印总和的值作为结果平均值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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 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;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to find the mean
// of pair product array of arr[]
static double  pairProductMean(int arr[],
                               int N)
{
  // Store product of pairs
  Vector pairArray =
         new Vector<>();
 
  // 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.add(pairProduct);
    }
  }
 
  // Size of pairArray
  int length = pairArray.size();
 
  // Store sum of pairArray
  float sum = 0;
  for (int i = 0; i < length; i++)
    sum += pairArray.get(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
public static void main(String[] args)
{
  // Given array arr[]
  int arr[] = {1, 2, 4, 8};
 
  int N = arr.length;
 
  // Function Call
 
  System.out.format("%.2f",
                    pairProductMean(arr, N));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the
# above approach
 
# Function to find the mean
# of pair product array of arr
def pairProductMean(arr, N):
   
    # Store product of pairs
    pairArray = [];
 
    # Generate all unordered
    # pairs
    for i in range(N):
        for j in range(i + 1, N):
            pairProduct = arr[i] * arr[j];
 
            # Store product of pairs
            pairArray.append(pairProduct);
 
    # Size of pairArray
    length = len(pairArray);
 
    # Store sum of pairArray
    sum = 0;
    for i in range(length):
        sum += pairArray[i];
 
    # Stores the mean of
    # pairArray
    mean = 0;
 
    # Find mean of pairArray
    if (length != 0):
        mean = sum / length;
    else:
        mean = 0;
 
    # Return the resultant
    # mean
    return mean;
 
# Driver Code
if __name__ == '__main__':
   
    # Given array arr
    arr = [1, 2, 4, 8];
 
    N = len(arr);
 
    # Function Call
    print("{0:.2f}".format(
            pairProductMean(arr, N)))
 
# This code is contributed by Rajput-Ji


C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to find the mean
// of pair product array of []arr
static double  pairProductMean(int []arr,
                               int N)
{
  // Store product of pairs
  List pairArray =
         new List();
 
  // 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.Add(pairProduct);
    }
  }
 
  // Size of pairArray
  int length = pairArray.Count;
 
  // 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
public static void Main(String[] args)
{
  // Given array []arr
  int []arr = {1, 2, 4, 8};
 
  int N = arr.Length;
 
  // Function Call
  Console.WriteLine("{0:F2}",
                    pairProductMean(arr,
                                    N));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program for the above approach
#include 
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;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
  
// Function to find the mean of pair
// product array of arr[]
static float pairProductMean(int arr[], int N)
{
     
    // Initializing suffix sum array
    int suffixSumArray[] = new int[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
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = { 1, 2, 4, 8 };
    int N = arr.length;
  
    // Function call
    System.out.format("%.2f",
                      pairProductMean(arr, N));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to find the mean of pair
# product array of arr[]
def pairProductMean(arr, N):
     
    # Initializing suffix sum array
    suffixSumArray = [0] * N
    suffixSumArray[N - 1] = arr[N - 1]
 
    # Build suffix sum array
    for i in range(N - 2, -1, -1):
        suffixSumArray[i] = suffixSumArray[i + 1] + arr[i]
 
    # Size of pairProductArray
    length = (N * (N - 1)) // 2
 
    # Stores sum of pairProductArray
    res = 0
 
    for i in range(N - 1):
        res += arr[i] * suffixSumArray[i + 1]
 
    # Store the mean
    mean = 0
 
    # Find mean of pairProductArray
    if (length != 0):
        mean = res / length
    else:
        mean = 0
 
    # Return the resultant mean
    return mean
 
# Driver Code
if __name__ == '__main__':
     
    # Given array arr[]
    arr = [ 1, 2, 4, 8 ]
    N = len(arr)
 
    # Function Call
    print(round(pairProductMean(arr, N), 2))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
  
class GFG{
      
// Function to find the mean of pair
// product array of arr[]
static double pairProductMean(int[] arr, int N)
{
     
    // Initializing suffix sum array
    int[] suffixSumArray = new int[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
    double res = 0;
   
    for(int i = 0; i < N - 1; i++)
    {
        res += arr[i] *
               suffixSumArray[i + 1];
    }
   
    // Store the mean
    double mean;
   
    // Find mean of pairProductArray
    if (length != 0)
        mean = res / length;
    else
        mean = 0;
   
    // Return the resultant mean
    return mean;
}
  
// Driver code
public static void Main()
{
     
    // Given array arr[]
    int[] arr = { 1, 2, 4, 8 };
    int N = arr.Length;
   
    // Function call
    Console.WriteLine(string.Format("{0:0.00}",
                      pairProductMean(arr, N)));
}
}
  
// This code is contributed by code_hunt


Javascript


输出
11.67

时间复杂度: O(N 2 )
辅助空间: O(N 2 )

有效的方法:我们的想法是使用的事实,每一个元素改编[I]乘以每个元素ARR [J]这是对元素ARR右侧的[I],在指数更正式的元素i乘以所有位于索引j处的元素使得j > i 。请按照以下步骤解决问题:

  • 为给定的数组arr[]创建一个后缀和数组suffixSumArray []
  • 初始化变量res以存储数组arr[]的乘积对的总和。
  • 迭代数组arr[]并为每个位置i递增resarr[i]*suffixSumArray[i+1]
  • 将变量resN*(N – 1)/ 2相除N*(N – 1)/ 2是可能产品的数量。
  • 最后,打印res的值作为结果平均值。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
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;
}

Java

// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
  
// Function to find the mean of pair
// product array of arr[]
static float pairProductMean(int arr[], int N)
{
     
    // Initializing suffix sum array
    int suffixSumArray[] = new int[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
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = { 1, 2, 4, 8 };
    int N = arr.length;
  
    // Function call
    System.out.format("%.2f",
                      pairProductMean(arr, N));
}
}
 
// This code is contributed by sanjoy_62

蟒蛇3

# Python3 program for the above approach
 
# Function to find the mean of pair
# product array of arr[]
def pairProductMean(arr, N):
     
    # Initializing suffix sum array
    suffixSumArray = [0] * N
    suffixSumArray[N - 1] = arr[N - 1]
 
    # Build suffix sum array
    for i in range(N - 2, -1, -1):
        suffixSumArray[i] = suffixSumArray[i + 1] + arr[i]
 
    # Size of pairProductArray
    length = (N * (N - 1)) // 2
 
    # Stores sum of pairProductArray
    res = 0
 
    for i in range(N - 1):
        res += arr[i] * suffixSumArray[i + 1]
 
    # Store the mean
    mean = 0
 
    # Find mean of pairProductArray
    if (length != 0):
        mean = res / length
    else:
        mean = 0
 
    # Return the resultant mean
    return mean
 
# Driver Code
if __name__ == '__main__':
     
    # Given array arr[]
    arr = [ 1, 2, 4, 8 ]
    N = len(arr)
 
    # Function Call
    print(round(pairProductMean(arr, N), 2))
 
# This code is contributed by mohit kumar 29

C#

// C# program for the above approach
using System;
  
class GFG{
      
// Function to find the mean of pair
// product array of arr[]
static double pairProductMean(int[] arr, int N)
{
     
    // Initializing suffix sum array
    int[] suffixSumArray = new int[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
    double res = 0;
   
    for(int i = 0; i < N - 1; i++)
    {
        res += arr[i] *
               suffixSumArray[i + 1];
    }
   
    // Store the mean
    double mean;
   
    // Find mean of pairProductArray
    if (length != 0)
        mean = res / length;
    else
        mean = 0;
   
    // Return the resultant mean
    return mean;
}
  
// Driver code
public static void Main()
{
     
    // Given array arr[]
    int[] arr = { 1, 2, 4, 8 };
    int N = arr.Length;
   
    // Function call
    Console.WriteLine(string.Format("{0:0.00}",
                      pairProductMean(arr, N)));
}
}
  
// This code is contributed by code_hunt

Javascript


输出
11.67

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live