Longest Bounded Subarray with One Exclusion — Problem Statement & Solution Guide
Problem Description
Given an array of positive integers nums and an integer limit, find the maximum length of a contiguous subarray such that the sum of its elements, after excluding exactly one occurrence of the maximum element in that subarray, is less than or equal to limit. If a subarray has a length of 1, its sum after excluding its only element is considered to be 0.
Examples
Input
[1, 2, 3, 4, 5], 10
Output
4
Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and limit 10, we first find the maximum element in the subarray, which is 5. Then, we exclude 5 from the subarray and calculate the sum of the remaining elements, which is 1 + 2 + 3 + 4 = 10. Since the sum is less than or equal to the limit, we return the length of the subarray, which is 4.
Input
[1, 2, 3, 4, 5], 5
Output
3
Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and limit 5, we first find the maximum element in the subarray, which is 5. Then, we exclude 5 from the subarray and calculate the sum of the remaining elements, which is 1 + 2 + 3 + 4 = 10. Since the sum is greater than the limit, we try excluding the next maximum element, which is 4. The sum of the remaining elements is 1 + 2 + 3 = 6, which is still greater than the limit. We continue this process until we find a subarray with a sum less than or equal to the limit. In this case, we exclude 4 and get a sum of 1 + 2 + 3 = 6, which is still greater than the limit. We then exclude 3 and get a sum of 1 + 2 + 4 = 7, which is still greater than the limit. Finally, we exclude 2 and get a sum of 1 + 3 + 4 = 8, which is still greater than the limit. We then exclude 1 and get a sum of 3 + 4 = 7, which is still greater than the limit. We then exclude 3 and get a sum of 1 + 4 = 5, which is less than or equal to the limit. Therefore, we return the length of the subarray, which is 3.
Constraints
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^9
- 0 <= limit <= 10^15
Optimal Approach & Strategy
Utilize a sliding window (two-pointer approach) combined with a monotonic deque to track the maximum value within the active window. As the 'right' pointer expands, we push the current index to the deque, maintaining descending order, and add the element to the running sum. If the current sum minus the maximum element (found at the front of the deque) exceeds the limit, we incrementally shrink the window from the 'left' until the constraint is satisfied, updating the maximum valid window size found.
Brute Force Approach
Check every possible subarray by iterating through all pairs of start and end indices. For each subarray, compute its total sum and locate the maximum element to verify if their difference is less than or equal to the limit. This approach has a time complexity of O(N^3) or O(N^2), which is too slow for an array of size 10^5.
Verified Code Solutions
function solution(nums, limit) {
let max = -Infinity;
let sum = 0;
let maxLength = 0;
for (let i = 0; i < nums.length; i++) {
max = Math.max(max, nums[i]);
sum += nums[i];
if (sum > limit) {
sum -= max;
max = -Infinity;
} else {
maxLength = Math.max(maxLength, i + 1);
}
}
return maxLength;
}class Solution {
public:
int solution(vector<int>& nums, int limit) {
int max = INT_MIN;
int sum = 0;
int maxLength = 0;
for (int i = 0; i < nums.size(); i++) {
max = max(max, nums[i]);
sum += nums[i];
if (sum > limit) {
sum -= max;
max = INT_MIN;
} else {
maxLength = max(maxLength, i + 1);
}
}
return maxLength;
}
};class Solution {
public int solution(int[] nums, int limit) {
int max = Integer.MIN_VALUE;
int sum = 0;
int maxLength = 0;
for (int i = 0; i < nums.length; i++) {
max = Math.max(max, nums[i]);
sum += nums[i];
if (sum > limit) {
sum -= max;
max = Integer.MIN_VALUE;
} else {
maxLength = Math.max(maxLength, i + 1);
}
}
return maxLength;
}
}def solution(nums, limit):
max_val = float('-inf')
current_sum = 0
max_length = 0
for num in nums:
max_val = max(max_val, num)
current_sum += num
if current_sum > limit:
current_sum -= max_val
max_val = float('-inf')
else:
max_length = max(max_length, len(nums[:nums.index(num) + 1]))
return max_lengthfunction solution(nums, limit) {
let max = -Infinity;
let sum = 0;
let maxLength = 0;
for (let i = 0; i < nums.length; i++) {
max = Math.max(max, nums[i]);
sum += nums[i];
if (sum > limit) {
sum -= max;
max = -Infinity;
} else {
maxLength = Math.max(maxLength, i + 1);
}
}
return maxLength;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.