Maximize the Total of Two Disjoint Fixed-Length Segments
Quick Overview
Maximize the Total of Two Disjoint Fixed-Length Segments. Choose two non-overlapping contiguous subarrays of nums, one of length firstLen and the other of length secondLen.
Maximize the Total of Two Disjoint Fixed-Length Segments
Company: Sprinklr
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Choose two non-overlapping contiguous subarrays of `nums`, one of length firstLen and the other of length secondLen. Either length may occur first in the array. Return their maximum combined sum. The segments may be adjacent or separated.
```cpp
class Solution {
public:
int maxSumTwoNoOverlap(vector<int>& nums, int firstLen, int secondLen);
};
```
### Examples
**Example 1:**
```text
Input: nums = [5,1,4,2,6], firstLen = 1, secondLen = 2
Output: 13
Explanation: Use singleton[5] and the final pair[2,6], totaling13. The possible pair sums are6,5,6,and8; their best disjoint singletons give totals12,11,12,and13.
```
**Example 2:**
```text
Input: nums = [1,2,3,4], firstLen = 2, secondLen = 2
Output: 10
Explanation: Both required segments together use the full array, so[1,2] and[3,4] attain10.
```
### Constraints
- `1 <= firstLen,secondLen <= 1000`.
- `2 <= firstLen+secondLen <= 1000`.
- `firstLen+secondLen <= nums.length <= 1000`.
- `0 <= nums[i] <= 1000`.
### Contract considerations
The answer is a sum, not the selected indices or an arbitrary placement.
```hint Read the boundary condition
For each right window, cache the best eligible left window rather than testing every pair.
```
### Boundary reminders
The answer is a sum, not the selected indices or an arbitrary placement.
Overview: Maximize the Total of Two Disjoint Fixed-Length Segments. Choose two non-overlapping contiguous subarrays of nums, one of length firstLen and the other of length secondLen.
Maximize the Total of Two Disjoint Fixed-Length Segments
Sprinklr
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Choose two non-overlapping contiguous subarrays of nums, one of length firstLen and the other of length secondLen. Either length may occur first in the array. Return their maximum combined sum. The segments may be adjacent or separated.
class Solution {
public:
int maxSumTwoNoOverlap(vector<int>& nums, int firstLen, int secondLen);
};
Examples
Example 1:
Input: nums = [5,1,4,2,6], firstLen = 1, secondLen = 2
Output: 13
Explanation: Use singleton[5] and the final pair[2,6], totaling13. The possible pair sums are6,5,6,and8; their best disjoint singletons give totals12,11,12,and13.
Example 2:
Input: nums = [1,2,3,4], firstLen = 2, secondLen = 2
Output: 10
Explanation: Both required segments together use the full array, so[1,2] and[3,4] attain10.
Constraints
1 <= firstLen,secondLen <= 1000
.
2 <= firstLen+secondLen <= 1000
.
firstLen+secondLen <= nums.length <= 1000
.
0 <= nums[i] <= 1000
.
Contract considerations
The answer is a sum, not the selected indices or an arbitrary placement.
Boundary reminders
The answer is a sum, not the selected indices or an arbitrary placement.