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.666666667Input: 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++ code to implement the above approach #include <bits/stdc++.h> using namespace std;
double solving( int n, vector< int >& 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< int > 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 code to implement the above approach import java.util.*;
class GFG {
public static double solving( int n,
ArrayList<Integer> 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<Integer> A = new ArrayList<Integer>(
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 |
# 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# 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 |
<script> // JavaScript code for the above approach
function solving(n, A) {
let maximum = A[0];
let sum = 0;
// Find the maximum element
for (let 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
let N = 4;
let A = [17, 3, 5, -3];
let ans = solving(N, A);
// Setting precision value of
// get decimal value upto 10 places
document.write(ans.toPrecision(12))
// This code is contributed by Potta Lokesh
</script>
|
18.6666666667
Time Complexity: O(N).
Auxiliary Space: O(1)