Maximize the average sum of two non-empty subsequences
Last Updated :
21 Mar, 2022
Given an array A[] of size N, find two Sub-sequences a and b, such that the sum of the average of two subsequences is maximized and return max sum.
Examples:
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
Approach: The problem can be solved using the fact that the average of a group of numbers is always between the minimum and maximum values in that group. It can be proven that to get the maximum sum of averages of two subsequences, place the maximum number in one subsequence and the rest of the numbers into the other.
So, answer would be (sum of all elements – largest element ) / (size -1 ) + largest Element .
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
double solving( int n, vector< int >& A)
{
int maximum = A[0];
long long sum = 0;
for ( int i = 0; i < n; i++) {
if (A[i] > maximum)
maximum = A[i];
sum += A[i];
}
return 1.0 * (sum - maximum) / (n - 1)
+ maximum;
}
int main()
{
int N = 4;
vector< int > A = { 17, 3, 5, -3 };
double ans = solving(N, A);
cout << fixed << setprecision(10);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static double solving( int n,
ArrayList<Integer> A)
{
int maximum = A.get( 0 );
long sum = 0 ;
for ( int i = 0 ; i < n; i++) {
if (A.get(i) > maximum)
maximum = A.get(i);
sum += A.get(i);
}
return 1.0 * (sum - maximum) / (n - 1 ) + maximum;
}
public static void main(String[] args)
{
int N = 4 ;
ArrayList<Integer> A = new ArrayList<Integer>(
Arrays.asList( 17 , 3 , 5 , - 3 ));
double ans = solving(N, A);
System.out.print(String.format( "%.10f" , ans));
}
}
|
Python3
def solving(n, A):
maximum = A[ 0 ]
sum = 0
for i in range ( 0 , n):
if (A[i] > maximum):
maximum = A[i]
sum = sum + A[i]
return 1.0 * ( sum - maximum) / (n - 1 ) + maximum
N = 4
A = [ 17 , 3 , 5 , - 3 ]
ans = ( float )(solving(N, A))
print ( round (ans, 10 ))
|
C#
using System;
class GFG {
static double solving( int n, int [] A)
{
int maximum = A[0];
long sum = 0;
for ( int i = 0; i < n; i++) {
if (A[i] > maximum)
maximum = A[i];
sum += A[i];
}
return 1.0 * (sum - maximum) / (n - 1) + maximum;
}
public static void Main()
{
int N = 4;
int [] A = { 17, 3, 5, -3 };
double ans = solving(N, A);
Console.Write(Math.Round(
ans, 10, MidpointRounding.AwayFromZero));
}
}
|
Javascript
<script>
function solving(n, A) {
let maximum = A[0];
let sum = 0;
for (let i = 0; i < n; i++) {
if (A[i] > maximum)
maximum = A[i];
sum += A[i];
}
return 1.0 * (sum - maximum) / (n - 1)
+ maximum;
}
let N = 4;
let A = [17, 3, 5, -3];
let ans = solving(N, A);
document.write(ans.toPrecision(12))
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...