Maximize maximum possible subarray sum of an array by swapping with elements from another array
Last Updated :
22 Apr, 2021
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, -1, 4, 5 }, brr[] = { 1, 2, 3, 2 }
Output : 21
Explanation : Swapping arr[2] with brr[2] modifies arr[] to {7, 2, 3, 4, 5}
Maximum subarray sum of the array arr[] = 21
Input : N = 2, K = 2, arr[] = { -4, -4 }, brr[] = { 8, 8 }
Output : 16
Explanation: Swap arr[0] with brr[0] and arr[1] with brr[1] modifies arr[] to {8, 8}
Maximum sum subarray of the array arr[] = 16
Approach: The idea to solve this problem is that by swapping elements of array arr and brr, the elements within arr can also be swapped in three swaps. Below are some observations:
- If two elements in the array arr[] having indices i and j are needed to be swapped, then take any temporary element from array brr[], say at index k, and perform the following operations:
- Swap arr[i] and brr[k].
- Swap brr[k] and arr[j].
- Swap arr[i] and brr[k].
- Now elements between array arr[] and brr[] can be swapped within the array arr[] as well. Therefore, greedily arrange elements in array arr[] such that it contains all the positive integers in a continuous manner.
Follow the steps below to solve the problem:
- Store all elements of array arr[] and brr[] in another array crr[].
- Sort the array crr[] in descending order.
- Calculate the sum till the last index (less than N) in the array crr[] which contains a positive element.
- Print the sum obtained.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void maxSum( int * arr, int * brr, int N, int K)
{
vector< int > crr;
for ( int i = 0; i < N; i++) {
crr.push_back(arr[i]);
}
for ( int i = 0; i < K; i++) {
crr.push_back(brr[i]);
}
sort(crr.begin(), crr.end(),
greater< int >());
int sum = 0;
for ( int i = 0; i < N; i++) {
if (crr[i] > 0) {
sum += crr[i];
}
else {
break ;
}
}
cout << sum << endl;
}
int main()
{
int arr[] = { 7, 2, -1, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int brr[] = { 1, 2, 3, 2 };
int K = sizeof (brr) / sizeof (brr[0]);
maxSum(arr, brr, N, K);
}
|
Java
import java.util.*;
class GFG
{
static void maxSum( int arr[], int brr[], int N, int K)
{
Vector<Integer> crr = new Vector<Integer>();
for ( int i = 0 ; i < N; i++)
{
crr.add(arr[i]);
}
for ( int i = 0 ; i < K; i++)
{
crr.add(brr[i]);
}
Collections.sort(crr);
Collections.reverse(crr);
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (crr.get(i) > 0 )
{
sum += crr.get(i);
}
else
{
break ;
}
}
System.out.println(sum);
}
public static void main(String[] args)
{
int arr[] = { 7 , 2 , - 1 , 4 , 5 };
int N = arr.length;
int brr[] = { 1 , 2 , 3 , 2 };
int K = brr.length;
maxSum(arr, brr, N, K);
}
}
|
Python3
def maxSum(arr, brr, N, K):
crr = []
for i in range (N):
crr.append(arr[i])
for i in range (K):
crr.append(brr[i])
crr = sorted (crr)[:: - 1 ]
sum = 0
for i in range (N):
if (crr[i] > 0 ):
sum + = crr[i]
else :
break
print ( sum )
if __name__ = = '__main__' :
arr = [ 7 , 2 , - 1 , 4 , 5 ]
N = len (arr)
brr = [ 1 , 2 , 3 , 2 ]
K = len (brr)
maxSum(arr, brr, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void maxSum( int [] arr, int [] brr,
int N, int K)
{
List< int > crr = new List< int >();
for ( int i = 0; i < N; i++)
{
crr.Add(arr[i]);
}
for ( int i = 0; i < K; i++)
{
crr.Add(brr[i]);
}
crr.Sort();
crr.Reverse();
int sum = 0;
for ( int i = 0; i < N; i++)
{
if (crr[i] > 0)
{
sum += crr[i];
}
else
{
break ;
}
}
Console.WriteLine(sum);
}
static void Main()
{
int [] arr = { 7, 2, -1, 4, 5 };
int N = arr.Length;
int [] brr = { 1, 2, 3, 2 };
int K = brr.Length;
maxSum(arr, brr, N, K);
}
}
|
Javascript
<script>
function maxSum(arr, brr, N, K)
{
let crr = [];
for (let i = 0; i < N; i++)
{
crr.push(arr[i]);
}
for (let i = 0; i < K; i++)
{
crr.push(brr[i]);
}
crr.sort( function (a, b){ return a - b});
crr.reverse();
let sum = 0;
for (let i = 0; i < N; i++)
{
if (crr[i] > 0)
{
sum += crr[i];
}
else
{
break ;
}
}
document.write(sum);
}
let arr = [ 7, 2, -1, 4, 5 ];
let N = arr.length;
let brr = [ 1, 2, 3, 2 ];
let K = brr.length;
maxSum(arr, brr, N, K);
</script>
|
Time Complexity: O((N+K)*log(N+K))
Auxiliary Space: O(N+K)
Share your thoughts in the comments
Please Login to comment...