You are given an integer array nums and two integers k and l.
Choose two contiguous fragments of nums: one of length k and one of length l. The two fragments may overlap (they may even occupy the same positions).
The chosen fragments determine a total:
an element covered by exactly one fragment adds its value;
an element covered by both fragments adds its negated value;
an element covered by neither fragment adds nothing.
Return the maximum total over all placements of the two fragments.
Examples
Example 1
Input: nums = [1, 3, -4, 2, -2], k = 3, l = 2
Output: 10
Explanation: Take the length-3 fragment at indices 0-2 and the length-2 fragment
at indices 2-3. Index 2 is covered by both fragments, so -4 contributes +4.
Total = 1 + 3 + 4 + 2 = 10.
Example 2
Input: nums = [-3], k = 1, l = 1
Output: 3
Explanation: Both fragments must cover the single element, which is therefore
negated: -(-3) = 3.
Constraints
1≤nums.length≤105
−1000≤nums[i]≤1000
1≤k,l≤nums.length
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up