Maximum Sum of Two Sign-Flipping Windows
Updated
Problem statement
You are given an integer array values and two window lengths, k and l. A window is a run of elements that sit next to each other. Choose one window of length k and one window of length l. The two windows may overlap.
- An element covered by exactly one chosen window contributes its original value.
- An element covered by both chosen windows contributes its value with the sign flipped.
- An element covered by neither window contributes nothing.
Return the largest possible total of all contributions.
Function
maxSignFlippedSum(values: int[], k: int, l: int) → intExamples
Example 1
values = [1,3,-4,2,-2]k = 3l = 2return = 10Take the window of length k at indices 0..2 and the window of length l at indices 2..3. They share the value -4, which flips to 4. The total is 1 + 3 + 4 + 2 = 10.
Example 2
values = [5,-2,4]k = 2l = 2return = 11Take the windows that start at index 0 and index 1. They 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
More American Express interview questions
- Maximum Points in a Perimeter-Bounded RectangleOA · Seen Sep 2026
- Minimum Moves to Spread Stones Across a GridOA · Seen Sep 2026
- Largest Two Non-Overlapping Buildable SquaresOA · Seen Aug 2026
- Crop Text at a Whole-Word BoundaryOA · Seen Aug 2026
- Minimum Parking Roof LengthOA · Seen Aug 2026
- Minimum Pile Reduction StepsOA · Seen Aug 2026
- Disjoint Target-Sum TripletsPHONE SCREEN · Seen Mar 2025
Similar questions at other companies
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
- Maximum Game Score with Fixed Forward JumpsStripe · OAStripe interview questions
- Most Visited Sprint MarkerMicrosoft · OAMicrosoft interview questions
- Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
- Earliest Meeting Start on a 24-Hour ClockIMC Trading · OAIMC Trading interview questions
- Calculate the Sum After Range TogglesBlink Health · OA
- Optimal TransferSnowflake · OASnowflake interview questions
- Count Attack Vectors by Divisor RangeVisa · OAVisa interview questions
- Smallest Most-Lit Street PositionHudson River Trading · OAHudson River Trading interview questions