Maximize Array sum by swapping at most K elements with another array
Last Updated :
08 Aug, 2022
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.
Maximize Array sum by swapping at most K elements with another array using Sorting
The idea is to replace all the smallest elements present in A with the largest elements of B, till the value of A[i] < B[i] and K swaps are not complete.
Follow the steps mentioned below to implement the idea:
- Sort the array A and B in non-decreasing order.
- Traverse 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.
- If the element of array A is smaller than that of array B, swap them. Otherwise, break the loop.
- Do this for at most K elements, and break the loop after that.
- Find the sum of the resultant array A.
Below is the implementation of the above approach:
C++
// C++ implementation to find maximum
// sum of array A by swapping
// at most K elements with array B
#include <bits/stdc++.h>
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
// 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
# 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#
// 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
JavaScript
<script>
// JavaScript implementation to find maximum
// sum of array A by swapping
// at most K elements with array B
// Function to find the maximum sum
function maximumSum(a, b, k, n)
{
let i, j;
a.sort();
b.sort();
// 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])
{
let temp = a[i];
a[i] = b[j];
b[j] = temp;
}
else
break;
}
// Find sum of resultant array
let sum = 0;
for (i = 0; i < n; i++)
sum += a[i];
document.write(sum);
}
let K = 1;
let A = [2, 3, 4 ];
let B = [ 6, 8, 5 ];
let N = A.length;
maximumSum(A, B, K, N);
// This code is contributed by vaibhavrabadiya117.
</script>
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Similar Reads
Maximize maximum possible subarray sum of an array by swapping with elements from another array Given two arrays arr[] and brr[] consisting of N and K elements respectively, the task is to find the maximum subarray sum possible from the array arr[] by swapping any element from the array arr[] with any element of the array brr[] any number of times. Examples: Input: N = 5, K = 4, arr[] = { 7, 2
7 min read
Maximize number of elements from Array with sum at most K Given an array A[] of N integers and an integer K, the task is to select the maximum number of elements from the array whose sum is at most K. Examples: Input: A[] = {1, 12, 5, 111, 200, 1000, 10}, K = 50 Output: 4 Explanation: Maximum number of selections will be 1, 12, 5, 10 that is 1 + 12 + 5 + 1
6 min read
Maximize Array sum by adding multiple of another Array element in given ranges Given two arrays X[] and Y[] of length N along with Q queries each of type [L, R] that denotes the subarray of X[] from L to R. The task is to find the maximum sum that can be obtained by applying the following operation for each query: Choose an element from Y[]. Add multiples with alternate +ve an
15+ min read
Maximize Array sum by replacing any K elements by its modulo with any positive integer Given an array of positive integer arr[], and a number K. the task is to maximize the sum of the array by replacing any K elements of the array by taking modulus with any positive integer which is less than arr[i] i.e, (arr[i] = arr[i]%X where X ⤠arr[i]). Examples: Input: arr[] = {5, 7, 18, 12, 11,
5 min read
Minimize the maximum of Array by replacing any element with other element at most K times Given an array arr[] of size N and an integer K, the task is to minimize the value of the maximum element of the array arr[] after replacing any element of the array with any other element of that array at most K times.Examples:Input: arr[] = {5, 3, 3, 2, 1}, K = 3Output: 2Explanation: Replace the e
8 min read
Minimize cost to bring maximum element at Kth position by swapping Given two integers N and K and an array of N positive integers (i.e., a0, a1, a2...., an-1), the task is to find the minimum cost to bring maximum value to Kth position by swapping (1-based indexing). The cost of swapping the elements is defined as the sum of values of swapping elements. Example: In
5 min read