Problem
Maximum Subarray Sum After Swaps
You are given an integer array a and an integer k.
You may perform at most k swap operations. In one swap, choose any two indices i and j and swap a[i] with a[j].
After performing at most k swaps, return the maximum possible subarray sum.
Examples
01 Β· Example 1
a = [1,-5,2] k = 1 return = 3
Choose the subarray [-5,2] and swap -5 with the outside value 1. The resulting chosen subarray has sum 1 + 2 = 3.
02 Β· Example 2
a = [4,-10,3,2] k = 1 return = 9
Choose the subarray [4,-10,3] and swap -10 with the outside value 2. The subarray sum becomes 4 + 2 + 3 = 9.
03 Β· Example 3
a = [-2,3,-1] k = 0 return = 3
No swaps are allowed, so this is the usual maximum subarray sum.
Constraints
2 <= a.length <= 5000 <= k <= 500-1000 <= a[i] <= 1000
More infosys problems
public int maximumSubarraySumAfterSwaps(int[] a, int k) {
// write your code here
}a[1,-5,2]
k1
expected3
sign in to submit