Problem · Array
Maximum Sum of Two Sign-Flipping Windows
Learn this problemProblem statement
Given an integer array values, choose one contiguous window of length k and one contiguous window of length l. The windows may overlap.
- An element covered by exactly one chosen window contributes its original value.
- An element covered by both chosen windows contributes its negated value.
- An element covered by neither window contributes nothing.
Return the maximum possible total contribution.
Function
maxSignFlippedSum(values: int[], k: int, l: int) → intExamples
Example 1
values = [1,3,-4,2,-2]k = 3l = 2return = 10Choose indices 0..2 and 2..3. The shared value -4 becomes 4, giving 1 + 3 + 4 + 2 = 10.
Example 2
values = [5,-2,4]k = 2l = 2return = 11The windows starting at 0 and 1 overlap on -2, which contributes 2; the total is 5 + 2 + 4 = 11.
Constraints
1 <= values.length <= 1000-10^4 <= values[i] <= 10^41 <= k <= values.length1 <= l <= values.length