BackmediumArraysOracle

Longest Bounded Subarray with One Exclusion Solution

Problem Statement

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.

Example 1
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.

Example 2
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
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Longest Bounded Subarray with One Exclusion — Problem Statement & Solution Guide

ArraysMediumPrefix Sum
TimeO(N)
|
SpaceO(N)

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

Example 1

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.

Example 2

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

JavaScript Solution
Time: O(N)
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;
   }

Asked in Top Tech Interviews

Oracle

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.