Problem · Array

Maximum Sum of Two Sign-Flipping Windows

Learn this problem
MediumAmerican Express logoAmerican ExpressINTERNOA

Problem 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) → int

Examples

Example 1

values = [1,3,-4,2,-2]k = 3l = 2return = 10

Choose 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 = 11

The 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^4
  • 1 <= k <= values.length
  • 1 <= l <= values.length

More American Express problems

drafts saved locally
public int maxSignFlippedSum(int[] values, int k, int l) {
    // Write your solution here.
}
values[1,3,-4,2,-2]
k3
l2
expected10
checking account