BackmediumArraysSalesforceMicrosoft

Longest Constrained Subsequence Solution

Problem Statement

Given a non-empty array of integers resource_values and an integer threshold, find the length of the longest contiguous subarray where the sum of the element values does not exceed threshold.

Example 1
Input
[1, 2, 3, 4, 5], 7
Output
3

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and threshold 7, we initialize a sliding window with [1, 2, 3] and sum 6. Then, we slide the window to the right by adding 4 and removing 1, resulting in a sum of 9, which exceeds the threshold. So, we slide the window to the right by adding 5 and removing 2, resulting in a sum of 8, which still exceeds the threshold. Finally, we slide the window to the right by adding 5 and removing 3, resulting in a sum of 7, which does not exceed the threshold. Therefore, the longest contiguous subarray with sum not exceeding 7 is [3, 4, 5] with sum 12, but the second longest is [1, 2, 3] with sum 6, so the output is 3.

Example 2
Input
[1, 2, 3, 4, 5], 3
Output
3

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and threshold 3, we initialize a sliding window with [1, 2] and sum 3. Then, we slide the window to the right by adding 3 and removing 1, resulting in a sum of 4, which exceeds the threshold. So, we slide the window to the right by adding 3 and removing 2, resulting in a sum of 3, which does not exceed the threshold. Therefore, the longest contiguous subarray with sum not exceeding 3 is [1, 2] with sum 3, so the output is 3.

Constraints

  • {"description":"1 <= length of resource_values <= 10^5","value":"10^5"}
  • {"description":"-10^9 <= each element in resource_values <= 10^9","value":"10^9"}
  • {"description":"0 <= threshold <= 10^9","value":"10^9"}
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 Constrained Subsequence — Problem Statement & Solution Guide

ArraysMediumSliding Window
TimeO(n)
|
SpaceO(1)

Problem Description

Given a non-empty array of integers resource_values and an integer threshold, find the length of the longest contiguous subarray where the sum of the element values does not exceed threshold.

Examples

Example 1

Input

[1, 2, 3, 4, 5], 7

Output

3

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and threshold 7, we initialize a sliding window with [1, 2, 3] and sum 6. Then, we slide the window to the right by adding 4 and removing 1, resulting in a sum of 9, which exceeds the threshold. So, we slide the window to the right by adding 5 and removing 2, resulting in a sum of 8, which still exceeds the threshold. Finally, we slide the window to the right by adding 5 and removing 3, resulting in a sum of 7, which does not exceed the threshold. Therefore, the longest contiguous subarray with sum not exceeding 7 is [3, 4, 5] with sum 12, but the second longest is [1, 2, 3] with sum 6, so the output is 3.

Example 2

Input

[1, 2, 3, 4, 5], 3

Output

3

Explanation: Step-by-step: with input [1, 2, 3, 4, 5] and threshold 3, we initialize a sliding window with [1, 2] and sum 3. Then, we slide the window to the right by adding 3 and removing 1, resulting in a sum of 4, which exceeds the threshold. So, we slide the window to the right by adding 3 and removing 2, resulting in a sum of 3, which does not exceed the threshold. Therefore, the longest contiguous subarray with sum not exceeding 3 is [1, 2] with sum 3, so the output is 3.

Constraints

  • {"description":"1 <= length of resource_values <= 10^5","value":"10^5"}
  • {"description":"-10^9 <= each element in resource_values <= 10^9","value":"10^9"}
  • {"description":"0 <= threshold <= 10^9","value":"10^9"}

Optimal Approach & Strategy

The optimal approach involves using a sliding window technique with two pointers, which allows us to find the solution in linear time complexity. By maintaining a running sum of the resources within the current window, we can efficiently expand and contract the window as needed.

Brute Force Approach

A naive approach would involve checking every possible subarray, which would result in a time complexity of O(n²). This can be achieved by using two nested loops to generate all possible subarrays and then checking each one's sum.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function longestConstrainedSubsequence(resource_values, threshold) {
  let maxLen = 0, windowSum = 0, left = 0;
  for (let right = 0; right < resource_values.length; right++) {
    windowSum += resource_values[right];
    while (windowSum > threshold) {
      windowSum -= resource_values[left];
      left++;
    }
    maxLen = Math.max(maxLen, right - left + 1);
  }
  return maxLen;
}

Asked in Top Tech Interviews

SalesforceMicrosoft

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.