#C8579. Longest Subarray with Sum Constraint

    ID: 52576 Type: Default 1000ms 256MiB

Longest Subarray with Sum Constraint

Longest Subarray with Sum Constraint

You are given an array of integers and a target integer \(k\). Your task is to determine the length of the longest contiguous subarray whose sum is less than or equal to \(k\). In other words, find the maximum length \(L\) such that there exists an index \(i\) with \(0 \leq i \leq n-L\) where

\(\sum_{j=i}^{i+L-1} a[j] \leq k\).

This problem requires an efficient approach, so a two-pointer (sliding window) technique is recommended.

inputFormat

The input is given via standard input (stdin):

  • The first line contains two space-separated integers \(n\) and \(k\): \(n\) is the number of elements in the array, and \(k\) is the target sum.
  • The second line contains \(n\) space-separated integers representing the elements of the array.

outputFormat

Output a single integer denoting the length of the longest contiguous subarray whose sum is less than or equal to \(k\). The output should be written to standard output (stdout).

## sample
5 10
1 2 3 4 5
4