Maximize sum of Averages of Pairs made using elements of X [] and Y []
Given two arrays X[] and Y[] of length N each. You can make a pair by selecting exactly one element from X[] and Y[]. The task is to find the maximum sum of averages by making N pairs.
Examples:
Input: N = 3, X[] = {6, 5, 4}, Y[] = {3, 1, 2}
Output: 10
Explanation: One of the possible way to arrange pairs is: (4, 2) (5, 1) (6, 3), Which gives average as ((4 + 2) / 2), ((1 + 5) / 2), ((6 + 3) / 2), Which is equal to 3, 3 and 4 respectively. Total sum of average is: 3 + 3 + 4 = 10. Which is maximum possible.
Input: N = 5, X[] = {4, 8, 3, 2, 1}, Y[] = {2, 5, 1, 2, 3}
Output: 15
Explanation: It can be verified that no other arrangement of pairs give sum of average more than 15.
Approach: Implement the idea below to solve the problem:
The problem can be solved via counting number of odd elements present in X[] and Y[]. Formula for calculating maximum possible average will be: (Total sum of elements of X[] and Y[] – Absolute difference(count of odd elements in X[] – count of odd elements in Y[])) / 2.
The second term is subtracted because there will be that many pairs where one element is odd and the other is even. And in case of each such pair we lose the value equal to 0.5 from the sum of averages.
Illustration of approach:
Consider an example: N = 5, X[] = {4, 8, 3, 2, 1}, Y[] = {2, 5, 1, 2, 3}
- Total sum of elements of X[] = 4 + 8 + 3 + 2 + 1 = 18
- Total sum of elements of Y[] = 2 + 5 + 1 + 2 + 3 = 13
- Total sum of both arrays = 18 + 13 = 31
- Count of odd elements in X[] = 2(3, 1)
- Count of odd elements in Y[] = 3(5, 1, 3)
- Total average of pairs will be:
(total sum of both arrays – absolute difference(odd count of X[] – odd count of Y[]))/2 = (31 – |2 – 3|) / 2 = (31 – 1) / 2 = 30 / 2 = 15.
This is the maximum possible sum of averages in this case.
Follow the steps mentioned below to solve the problem:
- Obtain the total sum of elements of both X[] and Y[].
- Count the number of odd elements in X[].
- Count the number of odd elements in Y[].
- Print the output according to discussed formula.
Below is the code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSumAverage( int X[], int Y[], int N)
{
int sum = 0;
int odd_countX = 0;
int odd_countY = 0;
for ( int i = 0; i < N; i++) {
sum += X[i];
sum += Y[i];
if (X[i] % 2 != 0) {
odd_countX++;
}
if (Y[i] % 2 != 0) {
odd_countY++;
}
}
return (sum - abs (odd_countX - odd_countY)) / 2;
}
int main()
{
int N = 5;
int X[5] = { 4, 8, 3, 2, 1 };
int Y[5] = { 2, 5, 1, 2, 3 };
cout << (maxSumAverage(X, Y, N));
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int maxSumAverage( int X[], int Y[],
int N)
{
int sum = 0 ;
int odd_countX = 0 ;
int odd_countY = 0 ;
for ( int i = 0 ; i < N; i++) {
sum += X[i];
sum += Y[i];
if (X[i] % 2 != 0 ) {
odd_countX++;
}
if (Y[i] % 2 != 0 ) {
odd_countY++;
}
}
return (sum - Math.abs(odd_countX - odd_countY))
/ 2 ;
}
public static void main(String[] args)
{
int N = 5 ;
int []X = { 4 , 8 , 3 , 2 , 1 };
int []Y = { 2 , 5 , 1 , 2 , 3 };
System.out.println(maxSumAverage(X, Y, N));
}
}
|
Python3
def maxSumAverage(X, Y, N):
Sum = 0
odd_countX = 0
odd_countY = 0
for i in range (N):
Sum + = X[i]
Sum + = Y[i]
if (X[i] % 2 ! = 0 ):
odd_countX + = 1
if (Y[i] % 2 ! = 0 ):
odd_countY + = 1
return ( Sum - abs (odd_countX - odd_countY)) / / 2
N = 5
X = [ 4 , 8 , 3 , 2 , 1 ]
Y = [ 2 , 5 , 1 , 2 , 3 ]
print (maxSumAverage(X, Y, N))
|
C#
using System;
public class GFG {
public static int maxSumAverage( int []X, int []Y,
int N)
{
int sum = 0;
int odd_countX = 0;
int odd_countY = 0;
for ( int i = 0; i < N; i++) {
sum += X[i];
sum += Y[i];
if (X[i] % 2 != 0) {
odd_countX++;
}
if (Y[i] % 2 != 0) {
odd_countY++;
}
}
return (sum - Math.Abs(odd_countX - odd_countY))
/ 2;
}
public static void Main( string [] args)
{
int N = 5;
int [] X = { 4, 8, 3, 2, 1 };
int [] Y = { 2, 5, 1, 2, 3 };
Console.WriteLine(maxSumAverage(X, Y, N));
}
}
|
Javascript
function maxSumAverage(X, Y, N)
{
let sum = 0;
let odd_countX = 0;
let odd_countY = 0;
for (let i = 0; i < N; i++) {
sum += X[i];
sum += Y[i];
if (X[i] % 2 != 0) {
odd_countX++;
}
if (Y[i] % 2 != 0) {
odd_countY++;
}
}
return (sum - Math.abs(odd_countX - odd_countY))
/ 2;
}
let N = 5;
let X = [4, 8, 3, 2, 1];
let Y = [2, 5, 1, 2, 3];
console.log(maxSumAverage(X, Y, N));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
14 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...