As reported: Salesforce MTS online assessment (India, general software engineer assessment, Nov 2025), question 1 of 2 in 75 minutes.
You are given an integer array arr and two integers minLength and threshold. Choose contiguous subarrays of arr such that:
- each chosen subarray has length at least
minLength, - each chosen subarray has sum at least
threshold, - no two chosen subarrays share an index.
Return the maximum number of subarrays you can choose.
Example 1
arr = [5,7,9,12,10,13], minLength = 2, threshold = 15
Output: 2
For example [5,7,9] (sum 21) and [12,10] (sum 22). The last element 13 is too short on its own.
Example 2
arr = [4,4,4,4], minLength = 1, threshold = 4
Output: 4
Constraints: 1 ≤ arr.length ≤ 10^5; -10^4 ≤ arr[i] ≤ 10^4; 1 ≤ minLength ≤ arr.length; -10^9 ≤ threshold ≤ 10^9.
Clarifications
- Elements not covered by any chosen subarray are allowed; the subarrays do not have to cover the array.
- Return
0when no valid subarray exists.
Code runner format: one JSON value per line: arr, minLength, threshold. Implement maxSubarrays(arr, minLength, threshold); the harness prints the count.