📌  相关文章
📜  通过将给定的 Array 划分为它们形成的两个子集的平均值之和最大化

📅  最后修改于: 2022-05-13 01:56:09.191000             🧑  作者: Mango

通过将给定的 Array 划分为它们形成的两个子集的平均值之和最大化

给定一个大小为 N 的数组arr[] ,任务是找到给定数组的2 个非空子集最大均值和,使得每个元素都是其中一个子集的一部分。

例子

方法:可以使用一些观察来解决该任务可以观察到,如果一个子集仅包含最大元素而另一个子集包含其余元素,则可以实现 2 个非空子集的最大均值和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return the maximum sum of the
// mean of the sum of two subsets of an array
double maxMeanSubsetSum(int arr[], int N)
{
    // Sorting the array
    sort(arr, arr + N);
 
    // Stores the sum of array
    double sum = 0;
 
    // Loop through the array and store
    // sum of elements except the largest
    for (int i = 0; i < N - 1; i++) {
        sum += arr[i];
    }
 
    // Calculating the mean
    sum = sum / (N - 1);
 
    // Adding the largest number
    // to the calculated mean
    sum += arr[N - 1];
    return sum;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = 5;
 
    // Giving output correct to
    // two decimal places
    cout << setprecision(2) << fixed
         << maxMeanSubsetSum(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to return the maximum sum of the
  // mean of the sum of two subsets of an array
  static double maxMeanSubsetSum(int arr[], int N)
  {
     
    // Sorting the array
    Arrays.sort(arr);
 
    // Stores the sum of array
    double sum = 0;
 
    // Loop through the array and store
    // sum of elements except the largest
    for (int i = 0; i < N - 1; i++) {
      sum += arr[i];
    }
 
    // Calculating the mean
    sum = sum / (N - 1);
 
    // Adding the largest number
    // to the calculated mean
    sum += arr[N - 1];
    return sum;
  }
 
  // Driver code
  public static void main (String[] args) {
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = 5;
 
    // Giving output correct to
    // two decimal places
    System.out.print(String.format("%.2f", maxMeanSubsetSum(arr, N)));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code for the above approach
 
# Function to return the maximum sum of the
# mean of the sum of two subsets of an array
def maxMeanSubsetSum(arr, N):
 
    # Sorting the array
    arr.sort()
 
    # Stores the sum of array
    sum = 0
 
    # Loop through the array and store
    # sum of elements except the largest
    for i in range(N - 1):
        sum += arr[i]
 
    # Calculating the mean
    sum = sum / (N - 1)
 
    # Adding the largest number
    # to the calculated mean
    sum = sum + arr[N - 1]
    return sum
 
# Driver code
arr = [1, 2, 3, 4, 5]
N = 5
 
# Giving output correct to
# two decimal places
print("{0:.2f}".format(maxMeanSubsetSum(arr, N)))
 
# This code is contributed by Potta Lokesh


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
  // Function to return the maximum sum of the
  // mean of the sum of two subsets of an array
  static double maxMeanSubsetSum(int[] arr, int N)
  {
 
    // Sorting the array
    Array.Sort(arr);
 
    // Stores the sum of array
    double sum = 0;
 
    // Loop through the array and store
    // sum of elements except the largest
    for (int i = 0; i < N - 1; i++) {
      sum += arr[i];
    }
 
    // Calculating the mean
    sum = sum / (N - 1);
 
    // Adding the largest number
    // to the calculated mean
    sum += arr[N - 1];
    return sum;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int[] arr = { 1, 2, 3, 4, 5 };
    int N = 5;
 
    // Giving output correct to
    // two decimal places
    Console.Write(string.Format("{0:0.00}", maxMeanSubsetSum(arr, N)));
  }
}
 
// This code is contributed by code_hunt.


Javascript



输出
7.50

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