#K94877. Longest Consecutive Subsequence with Sum Constraint
Longest Consecutive Subsequence with Sum Constraint
Longest Consecutive Subsequence with Sum Constraint
You are given an array of positive integers \(a_1, a_2, \ldots, a_n\) and a positive integer target
. Your task is to determine the length of the longest consecutive (contiguous) subsequence such that the sum of its elements is less than or equal to target
. In other words, find the maximum length \(L\) for which there exists an index \(i\) such that
\(\sum_{j=i}^{i+L-1} a_j \leq \text{target}\)
It is guaranteed that all array elements are positive integers.
inputFormat
The input is given from stdin and consists of two lines:
- The first line contains two space-separated integers:
n
(the length of the array) andtarget
. - The second line contains
n
space-separated positive integers representing the array.
outputFormat
Output a single integer representing the length of the longest consecutive subsequence whose sum is less than or equal to target
.
The answer should be printed to stdout.
## sample5 11
1 2 3 4 5
4