Given an array arr[] of N integers and an integer X, the task is to find the maximum possible sub-array sum after applying at most X swaps.
Examples:
Input: arr[] = {5, -1, 2, 3, 4, -2, 5}, X = 2
Output: 19
Swap (arr[0], arr[1]) and (arr[5], arr[6]).
Now, the maximum sub-array sum will be (5 + 2 + 3 + 4 + 5) = 19
Input: arr[] = {-2, -3, -1, -10}, X = 10
Output: -1
Approach: For every possible sub-array, consider the elements which are not part of this sub-array as discarded. Now, while there are swaps left and the sum of the sub-array currently under consideration can be maximized i.e. the greatest element among the discarded elements can be swapped with the minimum element of the sub-array, keep updating the sum of the sub-array. When there are no swaps left or the sub-array sum cannot be further maximized, update the current maximum sub-array sum found so far which will be the required answer in the end.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int SubarraySum( int a[], int n, int x)
{
int ans = -10000;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
int curans = 0;
priority_queue< int , vector< int > > pq;
priority_queue< int , vector< int >, greater< int > > pq2;
for ( int k = 0; k < n; k++) {
if (k >= i && k <= j) {
curans += a[k];
pq2.push(a[k]);
}
else
pq.push(a[k]);
}
ans = max(ans, curans);
for ( int k = 1; k <= x; k++) {
if (pq.empty() || pq2.empty()
|| pq2.top() >= pq.top())
break ;
curans -= pq2.top();
pq2.pop();
curans += pq.top();
pq.pop();
ans = max(ans, curans);
}
}
}
return ans;
}
int main()
{
int a[] = { 5, -1, 2, 3, 4, -2, 5 }, x = 2;
int n = sizeof (a) / sizeof (a[0]);
cout << SubarraySum(a, n, x);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int SubarraySum( int [] a, int n, int x)
{
int ans = - 10000 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i; j < n; j++)
{
int curans = 0 ;
ArrayList<Integer> pq = new ArrayList<Integer>();
ArrayList<Integer> pq2 = new ArrayList<Integer>();
for ( int k = 0 ; k < n; k++) {
if (k >= i && k <= j) {
curans += a[k];
pq2.add(a[k]);
}
else
pq.add(a[k]);
}
Collections.sort(pq);
Collections.reverse(pq);
Collections.sort(pq2);
ans = Math.max(ans, curans);
for ( int k = 1 ; k <= x; k++) {
if (pq.size() == 0 || pq2.size() == 0
|| pq2.get( 0 ) >= pq.get( 0 ))
break ;
curans -= pq2.get( 0 );
pq2.remove( 0 );
curans += pq.get( 0 );
pq.remove( 0 );
ans = Math.max(ans, curans);
}
}
}
return ans;
}
public static void main (String[] args)
{
int [] a = { 5 , - 1 , 2 , 3 , 4 , - 2 , 5 };
int x = 2 ;
int n = a.length;
System.out.println(SubarraySum(a, n, x));
}
}
|
Python3
def SubarraySum(a, n, x) :
ans = - 10000
for i in range (n) :
for j in range (i, n) :
curans = 0
pq = []
pq2 = []
for k in range (n) :
if (k > = i and k < = j) :
curans + = a[k]
pq2.append(a[k])
else :
pq.append(a[k])
pq.sort()
pq.reverse()
pq2.sort()
ans = max (ans, curans)
for k in range ( 1 , x + 1 ) :
if ( len (pq) = = 0 or len (pq2) = = 0 or pq2[ 0 ] > = pq[ 0 ]) :
break
curans - = pq2[ 0 ]
pq2.pop( 0 )
curans + = pq[ 0 ]
pq.pop( 0 )
ans = max (ans, curans)
return ans
a = [ 5 , - 1 , 2 , 3 , 4 , - 2 , 5 ]
x = 2 ;
n = len (a)
print (SubarraySum(a, n, x))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int SubarraySum( int [] a, int n, int x)
{
int ans = -10000;
for ( int i = 0; i < n; i++)
{
for ( int j = i; j < n; j++)
{
int curans = 0;
List< int > pq = new List< int >();
List< int > pq2 = new List< int >();
for ( int k = 0; k < n; k++) {
if (k >= i && k <= j) {
curans += a[k];
pq2.Add(a[k]);
}
else
pq.Add(a[k]);
}
pq.Sort();
pq.Reverse();
pq2.Sort();
ans = Math.Max(ans, curans);
for ( int k = 1; k <= x; k++) {
if (pq.Count == 0 || pq2.Count == 0
|| pq2[0] >= pq[0])
break ;
curans -= pq2[0];
pq2.RemoveAt(0);
curans += pq[0];
pq.RemoveAt(0);
ans = Math.Max(ans, curans);
}
}
}
return ans;
}
static void Main() {
int [] a = { 5, -1, 2, 3, 4, -2, 5 };
int x = 2;
int n = a.Length;
Console.WriteLine(SubarraySum(a, n, x));
}
}
|
Javascript
<script>
function SubarraySum(a, n, x)
{
let ans = -10000;
for (let i = 0; i < n; i++)
{
for (let j = i; j < n; j++)
{
let curans = 0;
let pq = [];
let pq2 = [];
for (let k = 0; k < n; k++) {
if (k >= i && k <= j) {
curans += a[k];
pq2.push(a[k]);
}
else
pq.push(a[k]);
}
pq.sort();
pq.reverse();
pq2.sort();
ans = Math.max(ans, curans);
for (let k = 1; k <= x; k++) {
if (pq.length == 0 || pq2.length == 0
|| pq2[0] >= pq[0])
break ;
curans -= pq2[0];
pq2.shift();
curans += pq[0];
pq.shift();
ans = Math.max(ans, curans);
}
}
}
return ans;
}
let a = [ 5, -1, 2, 3, 4, -2, 5 ];
let x = 2;
let n = a.length;
document.write(SubarraySum(a, n, x));
</script>
|
Time Complexity: O(n3 logn)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
15 Sep, 2022
Like Article
Save Article