最大化两个非空子序列的平均和
给定一个大小为N的数组A[] ,找到两个子序列a和b ,使得两个子序列的平均值之和最大化并返回最大和。
例子:
Input: N = 4, A[] = {17, 3, 5, -3}
Output:18.666666667
Explanation:
Let a =[3, 5, -3] and b = [17] then,
sum1= (3+5 -3)/3 = 5/3 = 1.666666667
sum2= 17/1 = 17
so, sum1+sum2 = 18.666666667
Input: N = 4, A[] = {1, 2, 3, 4}
Output: 5.333
方法:可以使用一组数字的平均值始终在该组中的最小值和最大值之间的事实来解决问题。可以证明,要获得两个子序列的最大平均值之和,请将最大数放在一个子序列中,将其余数字放在另一个子序列中。
So, answer would be (sum of all elements – largest element ) / (size -1 ) + largest Element .
下面是上述方法的实现。
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)