Shortest Combined Length of Two Disjoint Target-Sum Windows
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Find two nonempty,non-overlapping contiguous subarrays of arr, each summing to target. Return the smallest sum of their lengths,or -1 when no such pair exists. Return the length value rather than the windows themselves.
```cpp
class Solution {
public:
int minSumOfLengths(vector<int>& arr, int target);
};
```
### Examples
**Example 1:**
```text
Input: arr = [1,2,1,2], target = 3
Output: 4
Explanation: The first two and last two entries form disjoint target-sum windows. Every target-sum window needs at least two entries.
```
**Example 2:**
```text
Input: arr = [1,1,1], target = 2
Output: -1
Explanation: The two length2 target-sum windows overlap,so they cannot form a valid pair.
```
### Constraints
- arr has length 1 to 100000.
- `1 <= arr[i] <= 1000`.
- `1 <= target <= 100000000`.
### Contract considerations
The phrase multiple answers refers to window choices; the returned minimum integer is deterministic.
```hint Read the boundary condition
Both target-sum subarrays must be nonempty and disjoint. Minimize their combined length and account for a missing feasible pair.
```
### Boundary reminders
The phrase multiple answers refers to window choices; the returned minimum integer is deterministic.
Overview: Shortest Combined Length of Two Disjoint Target-Sum Windows. Find two nonempty,non-overlapping contiguous subarrays of arr, each summing to target.
Shortest Combined Length of Two Disjoint Target-Sum Windows
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Find two nonempty,non-overlapping contiguous subarrays of arr, each summing to target. Return the smallest sum of their lengths,or -1 when no such pair exists. Return the length value rather than the windows themselves.
class Solution {
public:
int minSumOfLengths(vector<int>& arr, int target);
};
Examples
Example 1:
Input: arr = [1,2,1,2], target = 3
Output: 4
Explanation: The first two and last two entries form disjoint target-sum windows. Every target-sum window needs at least two entries.
Example 2:
Input: arr = [1,1,1], target = 2
Output: -1
Explanation: The two length2 target-sum windows overlap,so they cannot form a valid pair.
Constraints
arr has length 1 to 100000.
1 <= arr[i] <= 1000
.
1 <= target <= 100000000
.
Contract considerations
The phrase multiple answers refers to window choices; the returned minimum integer is deterministic.
Boundary reminders
The phrase multiple answers refers to window choices; the returned minimum integer is deterministic.