#C11617. Longest Contiguous Subarray Under Maximum Load
Longest Contiguous Subarray Under Maximum Load
Longest Contiguous Subarray Under Maximum Load
You are given an array of integers representing weights and an integer M, the maximum allowed load. Your task is to determine the length of the longest contiguous subarray such that the sum of its elements is less than or equal to M.
More formally, given an array (W) of length (N) and an integer (M), find the maximum integer (L) such that there exists an index (i) with (0 \leq i \leq N - L) where (\sum_{j=i}^{i+L-1} W[j] \leq M).
This problem needs to be solved for multiple test cases.
inputFormat
The first line contains a single integer (T) denoting the number of test cases. Each test case consists of two lines:
- The first line contains two integers (N) and (M) where (N) is the number of weights and (M) is the maximum allowed load.
- The second line contains (N) space-separated integers representing the weights.
outputFormat
For each test case, print a single integer representing the length of the longest contiguous subarray with a sum less than or equal to (M). Each answer should be written on a new line.## sample
2
5 10
1 2 3 4 5
3 5
2 2 2
4
2
</p>