#C934. Longest Subarray with Sum At Most k

    ID: 53422 Type: Default 1000ms 256MiB

Longest Subarray with Sum At Most k

Longest Subarray with Sum At Most k

You are given an array of n integers and an integer k. Your task is to determine the length of the longest contiguous subarray such that the sum of its elements does not exceed k. If no subarray satisfies the condition, output 0.

Note: A contiguous subarray is a sequence of consecutive elements from the array.

The formula behind the problem can be summarized using the sliding window technique. Let the subarray be represented by indices \(i\) to \(j\) where \(0 \leq i \leq j < n\). The condition is:

[ \sum_{t=i}^{j} a_t \leq k ]

Your goal is to maximize \(j - i + 1\) under the above constraint.

inputFormat

The first line of the input contains two space-separated integers, n and k, where n is the size of the array and k is the maximum allowed sum.

The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single integer: the length of the longest contiguous subarray with a sum at most k. If no such subarray exists, output 0.

## sample
5 10
1 2 3 4 5
4

</p>