#C6415. Longest Subarray with Sum Constraint
Longest Subarray with Sum Constraint
Longest Subarray with Sum Constraint
You are given an array of integers and two numbers, N and K. Your task is to find the length of the longest contiguous subarray whose sum is less than or equal to K.
This can be mathematically formulated as finding the maximum integer \( L \) such that there exists an index \( i \) (0-indexed) satisfying:
\[ \sum_{j=i}^{i+L-1} arr[j] \leq K \]
The problem requires efficient handling of multiple test cases. Use a sliding window (two pointers) technique for an optimal solution.
inputFormat
The first line contains an integer T representing the number of test cases. Each test case consists of the following two lines:
- The first line contains two integers: N (the number of elements in the array) and K (the sum constraint).
- The second line contains N space-separated integers representing the array elements. If N is 0, this line will be empty.
Input is read from stdin
.
outputFormat
For each test case, output a single integer representing the length of the longest contiguous subarray with a sum less than or equal to K. Each result should be printed on a new line to stdout
.
2
5 10
1 2 3 4 5
8 15
3 1 2 1 1 1 1 1
4
8
</p>