#C571. Longest Subarray with Sum at Most K

    ID: 49389 Type: Default 1000ms 256MiB

Longest Subarray with Sum at Most K

Longest Subarray with Sum at Most K

Given an array of integers and a number \(k\), find the length of the longest contiguous subarray such that the sum of its elements is at most \(k\). Formally, let the array be \(A\) of length \(n\). You are required to determine the maximum integer \(L\) for which there exist indices \(i, j\) (with \(i \le j\)) such that \(j - i + 1 = L\) and \(A[i] + A[i+1] + \cdots + A[j] \le k\).

This problem tests your ability to efficiently manipulate subarrays and implement the sliding window technique.

inputFormat

The first line contains two integers (n) and (k) (with (0 \leq n \leq 10^5) and (0 \leq k \leq 10^9)), where (n) denotes the number of elements in the array and (k) is the maximum allowed sum. If (n > 0), the second line contains (n) space-separated integers representing the array elements.

outputFormat

Output a single integer representing the length of the longest contiguous subarray whose sum is at most (k).## sample

5 7
1 2 1 0 1
5