Maximize sum of means of two subsets formed by dividing given Array into them
Last Updated :
16 Feb, 2022
Given an array arr[] of size N, the task is to find the maximum sum of means of 2 non-empty subsets of the given array such that each element is part of one of the subsets.
Examples:
Input: N = 2, arr[] = {1, 3}
Output: 4.00
Explanation: Since there are only two elements, make two subsets as {1} and {3}.
Their sizes are 1 so mean for both will be 1.00 and 3.00 which sums to 4.00
Input: N = 5, arr[] = {1, 2, 3, 4, 5}
Output: 7.50
Explanation: Since there are 5 elements in the array, divide the array as {1, 2, 3, 4} and {5}.
Their sizes are 4 and 1 respectively. The mean for the first subset will be (1+2+3+4)/4 = 2.50
and the mean of the other subset will be 5. Hence the sum of means will be 2.50+5.00 = 7.50
For any other subset division the sum of means will not be more than 7.50.
Examples: {1, 2, 3} and {4, 5} the means will be (1+2+3)/3 and (4+5)/2
which is 2.00 and 4.50 respectively which sums to 6.50 which is less than 7.50
Approach: The task can be solved using some observations It can be observed that the maximum sum of means of 2 non-empty subsets can be achieved if one of the subsets contains only the maximum element & the other subset contains the rest of the elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double maxMeanSubsetSum( int arr[], int N)
{
sort(arr, arr + N);
double sum = 0;
for ( int i = 0; i < N - 1; i++) {
sum += arr[i];
}
sum = sum / (N - 1);
sum += arr[N - 1];
return sum;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = 5;
cout << setprecision(2) << fixed
<< maxMeanSubsetSum(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static double maxMeanSubsetSum( int arr[], int N)
{
Arrays.sort(arr);
double sum = 0 ;
for ( int i = 0 ; i < N - 1 ; i++) {
sum += arr[i];
}
sum = sum / (N - 1 );
sum += arr[N - 1 ];
return sum;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = 5 ;
System.out.print(String.format( "%.2f" , maxMeanSubsetSum(arr, N)));
}
}
|
Python3
def maxMeanSubsetSum(arr, N):
arr.sort()
sum = 0
for i in range (N - 1 ):
sum + = arr[i]
sum = sum / (N - 1 )
sum = sum + arr[N - 1 ]
return sum
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = 5
print ( "{0:.2f}" . format (maxMeanSubsetSum(arr, N)))
|
C#
using System;
public class GFG
{
static double maxMeanSubsetSum( int [] arr, int N)
{
Array.Sort(arr);
double sum = 0;
for ( int i = 0; i < N - 1; i++) {
sum += arr[i];
}
sum = sum / (N - 1);
sum += arr[N - 1];
return sum;
}
public static void Main(String[] args)
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = 5;
Console.Write( string .Format( "{0:0.00}" , maxMeanSubsetSum(arr, N)));
}
}
|
Javascript
<script>
const maxMeanSubsetSum = (arr, N) => {
arr.sort();
let sum = 0;
for (let i = 0; i < N - 1; i++) {
sum += arr[i];
}
sum = sum / (N - 1);
sum += arr[N - 1];
return sum;
}
let arr = [1, 2, 3, 4, 5];
let N = 5;
document.write(parseFloat(maxMeanSubsetSum(arr, N)).toFixed(2));
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...