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:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum ` `// sub-array sum after at most x swaps ` `int` `SubarraySum(` `int` `a[], ` `int` `n, ` `int` `x) ` `{ ` ` ` `// To store the required answer ` ` ` `int` `ans = -10000; ` ` ` ` ` `// For all possible intervals ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `for` `(` `int` `j = i; j < n; j++) { ` ` ` ` ` `// Keep current ans as zero ` ` ` `int` `curans = 0; ` ` ` ` ` `// To store the integers which are ` ` ` `// not part of the sub-array ` ` ` `// currently under consideration ` ` ` `priority_queue<` `int` `, vector<` `int` `> > pq; ` ` ` ` ` `// To store elements which are ` ` ` `// part of the sub-array ` ` ` `// currently under consideration ` ` ` `priority_queue<` `int` `, vector<` `int` `>, greater<` `int` `> > pq2; ` ` ` ` ` `// Create two sets ` ` ` `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); ` ` ` ` ` `// Swap at most X elements ` ` ` `for` `(` `int` `k = 1; k <= x; k++) { ` ` ` `if` `(pq.empty() || pq2.empty() ` ` ` `|| pq2.top() >= pq.top()) ` ` ` `break` `; ` ` ` ` ` `// Remove the minimum of ` ` ` `// the taken elements ` ` ` `curans -= pq2.top(); ` ` ` `pq2.pop(); ` ` ` ` ` `// Add maximum of the ` ` ` `// discarded elements ` ` ` `curans += pq.top(); ` ` ` `pq.pop(); ` ` ` ` ` `// Update the answer ` ` ` `ans = max(ans, curans); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the maximized sub-array sum ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

19

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.

## Recommended Posts:

- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Maximum Subarray Sum after inverting at most two elements
- Lexicographically smallest array after at-most K consecutive swaps
- Largest permutation after at most k swaps
- Lexicographical smallest number after at most K consecutive swaps
- Maximum possible GCD after replacing at most one element in the given array
- Maximize the subarray sum after multiplying all elements of any subarray with X
- First subarray having sum at least half the maximum sum of any subarray of size K
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Maximum sum subarray removing at most one element
- Maximum subarray sum by flipping signs of at most K array elements
- Largest lexicographic array with at-most K consecutive swaps
- Maximize distance between two elements of Array by at most X swaps
- Find lexicographically smallest string in at most one swaps
- Maximum Subset Sum possible by negating the entire sum after selecting the first Array element
- Maximum length of subarray such that sum of the subarray is even
- Maximum subarray sum in an array created after repeated concatenation
- Maximize the maximum subarray sum after removing atmost one element
- Maximum sum subarray after altering the array

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.