# Maximize Array sum by swapping at most K elements with another array

Given two arrays A and B of size N and an integer K, the task is to find the maximum possible sum of array A by swapping at most K elements with array B.

Examples:

Input: A[] = {2, 3, 4}, B[] = {6, 8, 5}, K = 1
Output: 15
Explanation:
Swap A[0] and B[1]. Hence sum = 8 + 3 + 4 = 15.

Input: A[] = {9, 7}, B[] = {5, 1}, K = 2
Output: 16
Explanation:
Since all the elements of array A are greater than the elements of array B, no swaps are required.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Sort the arrays A and B in non-decreasing order.
2. Iterate the array A from the beginning and array B from the end, so that we can swap the minimum element of array A with the maximum element of array B.
3. If the element of array A is smaller than that of array B, swap them. Otherwise, break the loop.
4. Do this for at most K elements.
5. Find the sum of the resultant array A.

Below is the implementation of the above approach.

 `// C++ implementation to find maximum ` `// sum of array A by swapping ` `// at most K elements with array B ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the maximum sum ` `void` `maximumSum(``int` `a[], ``int` `b[], ` `                ``int` `k, ``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``sort(a, a + n); ` `    ``sort(b, b + n); ` ` `  `    ``// If element of array a is ` `    ``// smaller than that of ` `    ``// array b, swap them. ` `    ``for` `(i = 0, j = n - 1; i < k; ` `         ``i++, j--) { ` `        ``if` `(a[i] < b[j]) ` `            ``swap(a[i], b[j]); ` `        ``else` `            ``break``; ` `    ``} ` ` `  `    ``// Find sum of resultant array ` `    ``int` `sum = 0; ` `    ``for` `(i = 0; i < n; i++) ` `        ``sum += a[i]; ` `    ``cout << sum << endl; ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `K = 1; ` `    ``int` `A[] = { 2, 3, 4 }; ` `    ``int` `B[] = { 6, 8, 5 }; ` ` `  `    ``int` `N = ``sizeof``(A) / ``sizeof``(A[0]); ` ` `  `    ``maximumSum(A, B, K, N); ` `    ``return` `0; ` `} `

 `// Java implementation to find maximum ` `// sum of array A by swapping ` `// at most K elements with array B ` `import` `java.util.*; ` `class` `GFG{ ` ` `  `// Function to find the maximum sum ` `static` `void` `maximumSum(``int` `a[], ``int` `b[], ` `                       ``int` `k, ``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``Arrays.sort(a); ` `    ``Arrays.sort(b); ` ` `  `    ``// If element of array a is ` `    ``// smaller than that of ` `    ``// array b, swap them. ` `    ``for` `(i = ``0``, j = n - ``1``; i < k; i++, j--) ` `    ``{ ` `        ``if` `(a[i] < b[j]) ` `        ``{ ` `            ``int` `temp = a[i]; ` `            ``a[i] = b[j]; ` `            ``b[j] = temp; ` `        ``} ` `        ``else` `            ``break``; ` `    ``} ` ` `  `    ``// Find sum of resultant array ` `    ``int` `sum = ``0``; ` `    ``for` `(i = ``0``; i < n; i++) ` `        ``sum += a[i]; ` `    ``System.out.print(sum +``"\n"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `K = ``1``; ` `    ``int` `A[] = { ``2``, ``3``, ``4` `}; ` `    ``int` `B[] = { ``6``, ``8``, ``5` `}; ` ` `  `    ``int` `N = A.length; ` ` `  `    ``maximumSum(A, B, K, N); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

 `# Python3 implementation to find maximum ` `# sum of array A by swapping ` `# at most K elements with array B ` ` `  `# Function to find the maximum sum ` `def` `maximumSum(a, b, k, n): ` ` `  `    ``a.sort() ` `    ``b.sort() ` ` `  `    ``# If element of array a is ` `    ``# smaller than that of ` `    ``# array b, swap them. ` `    ``i ``=` `0` `    ``j ``=` `n ``-` `1` `     `  `    ``while` `i < k: ` `        ``if` `(a[i] < b[j]): ` `            ``a[i], b[j] ``=` `b[j], a[i] ` `             `  `        ``else``: ` `            ``break` `             `  `        ``i ``+``=` `1` `        ``j ``-``=` `1` ` `  `    ``# Find sum of resultant array ` `    ``sum` `=` `0` `    ``for` `i ``in` `range` `(n): ` `        ``sum` `+``=` `a[i] ` `         `  `    ``print``(``sum``) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``K ``=` `1` `    ``A ``=` `[ ``2``, ``3``, ``4` `] ` `    ``B ``=` `[ ``6``, ``8``, ``5` `] ` ` `  `    ``N ``=` `len``(A) ` ` `  `    ``maximumSum(A, B, K, N) ` ` `  `# This code is contributed by chitranayal `

 `// C# implementation to find maximum ` `// sum of array A by swapping ` `// at most K elements with array B ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to find the maximum sum ` `static` `void` `maximumSum(``int` `[]a,  ` `                       ``int` `[]b, ` `                       ``int` `k, ``int` `n) ` `{ ` `    ``int` `i, j; ` `    ``Array.Sort(a); ` `    ``Array.Sort(b); ` ` `  `    ``// If element of array a is ` `    ``// smaller than that of ` `    ``// array b, swap them. ` `    ``for` `(i = 0, j = n - 1; i < k; i++, j--) ` `    ``{ ` `        ``if` `(a[i] < b[j]) ` `        ``{ ` `            ``int` `temp = a[i]; ` `            ``a[i] = b[j]; ` `            ``b[j] = temp; ` `        ``} ` `        ``else` `            ``break``; ` `    ``} ` ` `  `    ``// Find sum of resultant array ` `    ``int` `sum = 0; ` `    ``for` `(i = 0; i < n; i++) ` `        ``sum += a[i]; ` `    ``Console.Write(sum +``"\n"``); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `K = 1; ` `    ``int` `[]A = { 2, 3, 4 }; ` `    ``int` `[]B = { 6, 8, 5 }; ` ` `  `    ``int` `N = A.Length; ` ` `  `    ``maximumSum(A, B, K, N); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

Output:
```15
```

Performance Analysis:

• Time Complexity: O(N * log N)
• Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :