#K72092. Longest Subarray with Sum at Most K

    ID: 33676 Type: Default 1000ms 256MiB

Longest Subarray with Sum at Most K

Longest Subarray with Sum at Most K

You are given an array of non-negative integers and an integer ( k ). Your task is to find the length of the longest contiguous subarray such that the sum of its elements is less than or equal to ( k ).

For example, if the input array is [1, 2, 3, 4, 5] with ( k = 11 ), then the subarray [1, 2, 3, 4] has a sum of 10 which is ( \le 11 ) and its length is 4. This is the longest subarray satisfying the condition.

inputFormat

The input is given via standard input (stdin) in the following format:
1. The first line contains a positive integer ( n ), representing the number of elements in the array.
2. The second line contains ( n ) space-separated non-negative integers representing the array elements.
3. The third line contains an integer ( k ), the maximum allowed sum for the subarray.

outputFormat

Output a single integer to standard output (stdout): the length of the longest contiguous subarray whose sum is less than or equal to ( k ).## sample

5
1 2 3 4 5
11
4