Skip to main content
All Questions

Maximize the sum of two possibly overlapping subarrays of sizes K and L, negating overlapped elements.

Unlock detailed company stats for this questionUpgrade

Given an array A of n integers (both positive and negative), choose two contiguous fragments, one of size K and one of size L. The two fragments may overlap. Each element contributes to the final sum only once: if it lies in only one fragment it is added with its original sign; if it lies in both, it is added with its sign flipped (negated). Find the maximum possible sum.

Function signature: int solution(vector<int> &A, int K, int L)

Example: A = [1, 3, -4, 2, -2], K = 3, L = 2. Fragments [1, 3, -4] (indices 0-2) and [-4, 2] (indices 2-3). Index 2 (-4) is shared, so it contributes +4. Result: 1 + 3 + 4 + 2 = 10.

Closest LeetCode problem: 1031 (Maximum Sum of Two Non-Overlapping Subarrays), but this variant allows overlap with a sign-flip twist.

Format · Online assessment.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.