Maximum Subarray Sum After Swaps
Updated
Problem statement
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.
Function
maximumSubarraySumAfterSwaps(a: int[], k: int) → intExamples
Example 1
a = [1,-5,2]k = 1return = 3Choose the subarray [-5,2] and swap -5 with the outside value 1. The resulting chosen subarray has sum 1 + 2 = 3.
Example 2
a = [4,-10,3,2]k = 1return = 9Choose the subarray [4,-10,3] and swap -10 with the outside value 2. The subarray sum becomes 4 + 2 + 3 = 9.
Example 3
a = [-2,3,-1]k = 0return = 3No swaps are allowed, so this is the usual maximum subarray sum.
Constraints
2 <= a.length <= 5000 <= k <= 500-1000 <= a[i] <= 1000
More Infosys Online Assessment questions
- Lowest Common Ancestor with a Changing RootOA · Seen Nov 2026
- Minimum Leaf Path Sum over All RootsOA · Seen Nov 2026
- Split Array to Minimize the Largest SumOA · Seen Nov 2026
- Cherry PickupOA · Seen Nov 2026
- Lexicographically Smallest Grid Path with One Letter ChangeOA · Seen Nov 2026
- Minimum Cost to Supply Water with Wells and PipesOA · Seen Nov 2026
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3OA · Seen Nov 2026
- Minimum Rounds to Complete All TasksOA · Seen Nov 2026
- Count Equal-Distinct Character PartitionsOA · Seen Oct 2026
- Maximum Alternating-Index Packet ValueOA · Seen Oct 2026