Maximize sum of averages of subsequences of lengths lying in a given range
Last Updated :
08 Sep, 2021
Given an array A[] consisting of N integers and two integers X and Y, the task is to find the maximum sum of the averages of each subsequences obtained by splitting the array into subsequences of lengths lying in the range [X, Y].
Note: The values of X and Y are such that it is always possible to make such groups.
Examples:
Input: A[] = {4, 10, 6, 5}, X = 2, Y = 3
Output: 12.50
Explanation:
Divide the given array into two groups as {4, 10}, {6, 5} such that their sizes lies over the range [2, 3].
The average of the first group = (4 + 10) / 2 = 7.
The average of the second group = (6 + 5) / 2 = 5.5.
Therefore, the sum of average = 7 + 5.5 = 12.5, which is minimum among all possible groups.
Input: A[] = {3, 3, 1}
Output: 3.00
Approach: The given problem can be solved by using the Greedy Approach. The idea is to sort the array in ascending order and choose the groups of size X because the average is inversely proportional to the number of elements. Finally, add the remaining elements to the last group. Follow the below steps to solve the problem:
- Sort the given array arr[] in ascending order.
- Initialize the variables, say sum, res, and count as 0 to store the sum of array elements, result, and the count of elements in the current group.
- Iterate over the range [0, N] using the variable i and perform the following steps:
- Add the value of arr[i] to the variable sum and increase the value of count by 1.
- If the value of count is equal to X, and the remaining array elements can’t form a group then add them to the current group and add the average in the variable res.
- Otherwise, add the average of the group formed so far in the variable res.
- After completing the above steps, print the value of res as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxAverage( int A[], int N, int X,
int Y)
{
sort(A, A + N);
int sum = 0;
double res = 0;
int count = 0;
for ( int i = 0; i < N; i++) {
sum += A[i];
count++;
if (count == X) {
if (N - i - 1 < X) {
i++;
int cnt = 0;
while (i < N) {
cnt++;
sum += A[i];
i++;
}
X = X + cnt;
res += ( double )sum / double (X);
break ;
}
res += ( double )sum / double (X);
sum = 0;
count = 0;
}
}
cout << fixed << setprecision(2)
<< res << "\n" ;
}
int main()
{
int A[] = { 4, 10, 6, 5 };
int N = sizeof (A) / sizeof (A[0]);
int X = 2, Y = 3;
maxAverage(A, N, X, Y);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maxAverage( int A[], int N, int X,
int Y)
{
Arrays.sort(A);
int sum = 0 ;
double res = 0 ;
int count = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += A[i];
count++;
if (count == X)
{
if (N - i - 1 < X)
{
i++;
int cnt = 0 ;
while (i < N)
{
cnt++;
sum += A[i];
i++;
}
X = X + cnt;
res += ( double )sum / ( double )(X);
break ;
}
res += ( double )sum / ( double )(X);
sum = 0 ;
count = 0 ;
}
}
System.out.println(res);
}
public static void main(String[] args)
{
int A[] = { 4 , 10 , 6 , 5 };
int N = A.length;
int X = 2 , Y = 3 ;
maxAverage(A, N, X, Y);
}
}
|
Python3
def maxAverage(A,N,X,Y):
A.sort()
sum = 0
res = 0
count = 0
for i in range (N):
sum + = A[i]
count + = 1
if (count = = X):
if (N - i - 1 < X):
i + = 1
cnt = 0
while (i < N):
cnt + = 1
sum + = A[i]
i + = 1
X = X + cnt
res + = sum / X
break
res + = sum / X
sum = 0
count = 0
print (res)
if __name__ = = '__main__' :
A = [ 4 , 10 , 6 , 5 ]
N = len (A)
X = 2
Y = 3
maxAverage(A, N, X, Y)
|
C#
using System;
public class GFG{
static void maxAverage( int []A, int N, int X,
int Y)
{
Array.Sort(A);
int sum = 0;
double res = 0;
int count = 0;
for ( int i = 0; i < N; i++)
{
sum += A[i];
count++;
if (count == X)
{
if (N - i - 1 < X)
{
i++;
int cnt = 0;
while (i < N)
{
cnt++;
sum += A[i];
i++;
}
X = X + cnt;
res += ( double )sum / ( double )(X);
break ;
}
res += ( double )sum / ( double )(X);
sum = 0;
count = 0;
}
}
Console.WriteLine(res);
}
public static void Main(String[] args)
{
int []A = { 4, 10, 6, 5 };
int N = A.Length;
int X = 2, Y = 3;
maxAverage(A, N, X, Y);
}
}
|
Javascript
<script>
function maxAverage(A, N, X, Y) {
A.sort( function (a, b) { return a - b; })
let sum = 0;
let res = 0;
let count = 0;
for (let i = 0; i < N; i++) {
sum += A[i];
count++;
if (count == X) {
if (N - i - 1 < X) {
i++;
let cnt = 0;
while (i < N) {
cnt++;
sum += A[i];
i++;
}
X = X + cnt;
res += sum / X;
break ;
}
res += sum / X;
sum = 0;
count = 0;
}
}
document.write(res.toPrecision(3))
}
let A = [4, 10, 6, 5];
let N = A.length;
let X = 2, Y = 3;
maxAverage(A, N, X, Y);
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...