# Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array

Given two arrays of size N, and two numbers X and Y, the task is to maximize the sum by considering the below points:

- Pick
**x**values from the first array and**y**values from the second array such that the sum of X+Y values is maximum. - It is given that X + Y is equal to N.

**Examples:**

Input:arr1[] = {1, 4, 1}, arr2[] = {2, 5, 3}, N = 3, X = 2, Y = 1

Output:8

In order to maximize sum from 2 arrays,

pick 1st and 2nd element from first array and 3rd from second array.

Input:A[] = {1, 4, 1, 2}, B[] = {4, 3, 2, 5}, N = 4, X = 2, Y = 2

Output:14

**Approach: **A greedy approach can be used to solve the above problem. Below are the required steps:

- Find those elements of arrays first that have maximum value by finding the highest difference between elements of two arrays.
- For that, find the absolute difference between the value of the first and second array and then store it in some another array.
- Sort this array in decreasing order.
- While sorting, track the original positions of elements in the arrays.
- Now compare the elements of the two arrays and add the greater value to the maxAmount.
- If both have the same value, add an element of the first array if X is not zero else add an element of the second array.
- After traversing the arrays completely return the maxAmount calculated.

Below is the implementation of above approach :

## C++

`// C++ program to print the maximum ` `// possible sum from two arrays. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// class that store values of two arrays ` `// and also store their absolute difference ` `class` `triplet { ` `public` `: ` ` ` `int` `first; ` ` ` `int` `second; ` ` ` `int` `diff; ` ` ` `triplet(` `int` `f, ` `int` `s, ` `int` `d) ` ` ` `: first(f), second(s), diff(d) ` ` ` `{ ` ` ` `} ` `}; ` ` ` `// Compare function used to sort array in decreasing order ` `bool` `compare(triplet& a, triplet& b) ` `{ ` ` ` `return` `a.diff > b.diff; ` `// decreasing order ` `} ` ` ` `/// Function to find the maximum possible ` `/// sum that can be generated from 2 arrays ` `int` `findMaxAmount(` `int` `arr1[], ` `int` `arr2[], ` `int` `n, ` `int` `x, ` `int` `y) ` `{ ` ` ` `// vector where each index stores 3 things: ` ` ` `// Value of 1st array ` ` ` `// Value of 2nd array ` ` ` `// Their absolute difference ` ` ` `vector<triplet> v; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `triplet t(arr1[i], arr2[i], ` `abs` `(arr1[i] - arr2[i])); ` ` ` `v.push_back(t); ` ` ` `} ` ` ` ` ` `// sort according to their absolute difference ` ` ` `sort(v.begin(), v.end(), compare); ` ` ` ` ` `// it will store maximum sum ` ` ` `int` `maxAmount = 0; ` ` ` ` ` `int` `i = 0; ` ` ` ` ` `// Run loop for N times or ` ` ` `// value of X or Y becomes zero ` ` ` `while` `(i < n && x > 0 && y > 0) { ` ` ` ` ` `// if 1st array element has greater ` ` ` `// value, add it to maxAmount ` ` ` `if` `(v[i].first > v[i].second) { ` ` ` `maxAmount += v[i].first; ` ` ` `x--; ` ` ` `} ` ` ` ` ` `// if 2nd array element has greater ` ` ` `// value, add it to maxAmount ` ` ` `if` `(v[i].first < v[i].second) { ` ` ` `maxAmount += v[i].second; ` ` ` `y--; ` ` ` `} ` ` ` ` ` `// if both have same value, add element ` ` ` `// of first array if X is not zero ` ` ` `// else add element of second array ` ` ` `if` `(v[i].first == v[i].second) { ` ` ` `if` `(x > 0) { ` ` ` `maxAmount += v[i].first; ` ` ` `x--; ` ` ` `} ` ` ` `else` `if` `(y > 0) { ` ` ` `maxAmount += v[i].second; ` ` ` `y--; ` ` ` `} ` ` ` `} ` ` ` ` ` `// increment after picking element ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// add the remaining values ` ` ` `// of first array to maxAmount ` ` ` `while` `(i < v.size() && x--) { ` ` ` `maxAmount += v[i++].first; ` ` ` `} ` ` ` ` ` `// add the remaining values of ` ` ` `// second array to maxAmount ` ` ` `while` `(i < v.size() && y--) { ` ` ` `maxAmount += v[i++].second; ` ` ` `} ` ` ` ` ` `return` `maxAmount; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `A[] = { 1, 4, 1, 2 }; ` ` ` `int` `B[] = { 4, 3, 2, 5 }; ` ` ` `int` `n = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` ` ` `int` `X = 2, Y = 2; ` ` ` ` ` `cout << findMaxAmount(A, B, n, X, Y) << ` `"\n"` `; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to print the maximum

# possible sum from two arrays.

# Class that store values of two arrays

# and also store their absolute difference

class triplet:

def __init__(self, f, s, d):

self.first = f

self.second = s

self.diff = d

# Function to find the maximum possible

# sum that can be generated from 2 arrays

def findMaxAmount(arr1, arr2, n, x, y):

# vector where each index stores 3 things:

# Value of 1st array

# Value of 2nd array

# Their absolute difference

v = []

for i in range(0, n):

t = triplet(arr1[i], arr2[i],

abs(arr1[i] – arr2[i]))

v.append(t)

# sort according to their absolute difference

v.sort(key = lambda x: x.diff, reverse = True)

# it will store maximum sum

maxAmount, i = 0, 0

# Run loop for N times or

# value of X or Y becomes zero

while i < n and x > 0 and y > 0:

# if 1st array element has greater

# value, add it to maxAmount

if v[i].first > v[i].second:

maxAmount += v[i].first

x -= 1

# if 2nd array element has greater

# value, add it to maxAmount

if v[i].first < v[i].second:
maxAmount += v[i].second
y -= 1
# if both have same value, add element
# of first array if X is not zero
# else add element of second array
if v[i].first == v[i].second:
if x > 0:

maxAmount += v[i].first

x -= 1

elif y > 0:

maxAmount += v[i].second

y -= 1

# increment after picking element

i += 1

# add the remaining values

# of first array to maxAmount

while i < len(v) and x > 0:

maxAmount += v[i].first

i, x = i + 1, x – 1

# add the remaining values of

# second array to maxAmount

while i < len(v) and y > 0:

maxAmount += v[i].second

i, y = i + 1, y – 1

return maxAmount

# Driver Code

if __name__ == “__main__”:

A = [1, 4, 1, 2]

B = [4, 3, 2, 5]

n = len(A)

X, Y = 2, 2

print(findMaxAmount(A, B, n, X, Y))

# This code is contributed by Rituraj Jain

**Output:**

14

**Time complexity:** O(N log N)

## Recommended Posts:

- Maximize elements using another array
- Maximize array elements upto given number
- Maximum sum by picking elements from two arrays in order
- Maximize the median of the given array after adding K elements to the same array
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed
- Average of remaining elements after removing K largest and K smallest elements from array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Elements to be added so that all elements of a range are present in array
- Find elements larger than half of the elements in an array
- Replace array elements by sum of next two consecutive elements
- Find all elements in array which have at-least two greater elements
- Generate an array of K elements such that sum of elements is N and the condition a[i] < a[i+1] <= 2*a[i] is met | Set 2
- Count array elements that divide the sum of all other elements
- Count number of elements between two given elements in array
- Find elements of array using XOR of consecutive elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.