📜  最大化两个非空子序列的平均和

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

最大化两个非空子序列的平均和

给定一个大小为N的数组A[] ,找到两个子序列ab ,使得两个子序列的平均值之和最大化并返回最大和。

例子:

方法:可以使用一组数字的平均值始终在该组中的最小值和最大值之间的事实来解决问题。可以证明,要获得两个子序列的最大平均值之和,请将最大数放在一个子序列中,将其余数字放在另一个子序列中。

下面是上述方法的实现。

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
double solving(int n, vector& A)
{
    int maximum = A[0];
    long long sum = 0;
 
    // Find the maximum element
    for (int i = 0; i < n; i++) {
        if (A[i] > maximum)
            maximum = A[i];
 
        // Sum of all elements of the array
        sum += A[i];
    }
 
    // Calculating the answer
    // using the formula discussed above
    return 1.0 * (sum - maximum) / (n - 1)
           + maximum;
}
 
// Driver code
int main()
{
    int N = 4;
    vector A = { 17, 3, 5, -3 };
    double ans = solving(N, A);
 
    // Setting precision value of
    // get decimal value upto 10 places
    cout << fixed << setprecision(10);
    cout << ans;
    return 0;
}


Java
// JAVA code to implement the above approach
import java.util.*;
class GFG {
  public static double solving(int n,
                               ArrayList A)
  {
    int maximum = A.get(0);
    long sum = 0;
 
    // Find the maximum element
    for (int i = 0; i < n; i++) {
      if (A.get(i) > maximum)
        maximum = A.get(i);
 
      // Sum of all elements of the array
      sum += A.get(i);
    }
 
    // Calculating the answer
    // using the formula discussed above
    return 1.0 * (sum - maximum) / (n - 1) + maximum;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 4;
    ArrayList A = new ArrayList(
      Arrays.asList(17, 3, 5, -3));
    double ans = solving(N, A);
 
    // Setting precision value of
    // get decimal value upto 10 places
    System.out.print(String.format("%.10f", ans));
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python code to implement the above approach
def solving(n, A):
 
    maximum = A[0]
    sum = 0
 
    # Find the maximum element
    for i in range(0, n):
        if (A[i] > maximum):
            maximum = A[i]
 
        # Sum of all elements of the array
        sum = sum + A[i]
 
    # Calculating the answer
    # using the formula discussed above
    return 1.0 * (sum - maximum) / (n - 1) + maximum
 
# Driver code
N = 4
A = [17, 3, 5, -3]
ans = (float)(solving(N, A))
 
# Setting precision value of
# get decimal value upto 10 places
print(round(ans, 10))
 
# This code is contributed by Taranpreet


C#
// C# code to implement the above approach
using System;
 
class GFG {
  static double solving(int n, int[] A)
  {
    int maximum = A[0];
    long sum = 0;
 
    // Find the maximum element
    for (int i = 0; i < n; i++) {
      if (A[i] > maximum)
        maximum = A[i];
 
      // Sum of all elements of the array
      sum += A[i];
    }
 
    // Calculating the answer
    // using the formula discussed above
    return 1.0 * (sum - maximum) / (n - 1) + maximum;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 4;
    int[] A = { 17, 3, 5, -3 };
    double ans = solving(N, A);
 
    // Setting precision value of
    // get decimal value upto 10 places
    Console.Write(Math.Round(
      ans, 10, MidpointRounding.AwayFromZero));
  }
}
 
// This code is contributed by Samim Hossain Mondal


Javascript



输出
18.6666666667

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