# Maximize sum of Averages of Pairs made using elements of X [] and Y []

• Last Updated : 14 Dec, 2022

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++

```// C++ implementation

#include <bits/stdc++.h>
using namespace std;

// Function to find the
// maximum possible sum of averages
int maxSumAverage(int X[], int Y[], int N)
{

// Variable to hold sum of both arrays
int sum = 0;

// Counter for counting odd
// elements present in X[]
int odd_countX = 0;

// Counter for counting odd
// elements present in X[]
int odd_countY = 0;

// Loop for traversing on arrays
for (int i = 0; i < N; i++) {
sum += X[i];
sum += Y[i];

if (X[i] % 2 != 0) {

// Incrementing odd_countX
odd_countX++;
}

if (Y[i] % 2 != 0) {

// Incrementing odd_countX
odd_countY++;
}
}

// Printing output by calculating
// it from formula
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 };

// Function call
cout << (maxSumAverage(X, Y, N));
return 0;
}
// this code is contributed by ksam24000```

## Java

```// Java code to implement the approach

import java.util.*;

class GFG {

// Function to find the
// maximum possible sum of averages
public static int maxSumAverage(int X[], int Y[],
int N)
{
// Variable to hold sum of both arrays
int sum = 0;

// Counter for counting odd
// elements present in X[]
int odd_countX = 0;

// Counter for counting odd
// elements present in X[]
int odd_countY = 0;

// Loop for traversing on arrays
for (int i = 0; i < N; i++) {
sum += X[i];
sum += Y[i];

if (X[i] % 2 != 0) {

// Incrementing odd_countX
odd_countX++;
}

if (Y[i] % 2 != 0) {

// Incrementing odd_countX
odd_countY++;
}
}

// Printing output by calculating
// it from formula
return (sum - Math.abs(odd_countX - odd_countY))
/ 2;
}

// Driver function
public static void main(String[] args)
{
int N = 5;
int[]X = { 4, 8, 3, 2, 1 };
int[]Y = { 2, 5, 1, 2, 3 };

// Function call
System.out.println(maxSumAverage(X, Y, N));
}
}
```

## Python3

```# Python code to implement the approach

# Function to find the maximum
# possible sum of averages
def maxSumAverage(X, Y, N):
# Variable to hold sum of both arrays
Sum = 0

# Counter for counting odd
# elements present in X[]
odd_countX = 0

# Counter for counting odd
# elements present in Y[]
odd_countY = 0

# Loop for traversing on arrays
for i in range(N):
Sum += X[i]
Sum += Y[i]

if(X[i] % 2 != 0):
# Incrementing odd_countX
odd_countX += 1
if(Y[i] % 2 != 0):
# Incrementing odd_countY
odd_countY += 1

# returning output by
# calculating it from formula
return (Sum-abs(odd_countX - odd_countY))//2

N = 5
X = [4, 8, 3, 2, 1]
Y = [2, 5, 1, 2, 3]
# Function call
print(maxSumAverage(X, Y, N))

# This code is contributed by lokesh
```

## C#

```/******************************************************************************

Online C# Compiler.
Code, Compile, Run and Debug C# program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/

// C# code to implement the approach
using System;
public class GFG {

// Function to find the
// maximum possible sum of averages
public static int maxSumAverage(int []X, int []Y,
int N)
{

// Variable to hold sum of both arrays
int sum = 0;

// Counter for counting odd
// elements present in X[]
int odd_countX = 0;

// Counter for counting odd
// elements present in X[]
int odd_countY = 0;

// Loop for traversing on arrays
for (int i = 0; i < N; i++) {
sum += X[i];
sum += Y[i];

if (X[i] % 2 != 0) {

// Incrementing odd_countX
odd_countX++;
}

if (Y[i] % 2 != 0) {

// Incrementing odd_countX
odd_countY++;
}
}

// Printing output by calculating
// it from formula
return (sum - Math.Abs(odd_countX - odd_countY))
/ 2;
}

// Driver function
public static void Main(string[] args)
{
int N = 5;
int[] X = { 4, 8, 3, 2, 1 };
int[] Y = { 2, 5, 1, 2, 3 };

// Function call
Console.WriteLine(maxSumAverage(X, Y, N));
}
}

// This code is contributed by AnkThon```

## Javascript

```    // JavaScript code for the above approach
// Function to find the
// maximum possible sum of averages
function maxSumAverage(X, Y, N)
{

// Variable to hold sum of both arrays
let sum = 0;

// Counter for counting odd
// elements present in X[]
let odd_countX = 0;

// Counter for counting odd
// elements present in X[]
let odd_countY = 0;

// Loop for traversing on arrays
for (let i = 0; i < N; i++) {
sum += X[i];
sum += Y[i];

if (X[i] % 2 != 0) {

// Incrementing odd_countX
odd_countX++;
}

if (Y[i] % 2 != 0) {

// Incrementing odd_countX
odd_countY++;
}
}

// Printing output by calculating
// it from formula
return (sum - Math.abs(odd_countX - odd_countY))
/ 2;
}

// Driver function

let N = 5;
let X = [4, 8, 3, 2, 1];
let Y = [2, 5, 1, 2, 3];

// Function call
console.log(maxSumAverage(X, Y, N));

// This code is contributed by Potta Lokesh
```
Output

`15`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up