#K856. Maximum Fortunate Segment Length

    ID: 36677 Type: Default 1000ms 256MiB

Maximum Fortunate Segment Length

Maximum Fortunate Segment Length

You are given an integer n representing the number of elements in an array, an integer k, and an array of n positive integers. A contiguous segment (subarray) of the array is called a fortunate segment if:

  • The sum of its elements is greater than or equal to \( k \).
  • The segment is minimal in the sense that removing the leftmost element would cause the sum to drop below \( k \).

Your task is to determine the maximum length among all such minimal (fortunate) segments. If no such segment exists, output 0.

Note: The input guarantees that all array elements are positive, which ensures that the sliding window technique can be applied.

inputFormat

The first line of input contains two integers \( n \) and \( k \) separated by a space.

The second line contains \( n \) positive integers separated by spaces, representing the array elements.

For example:

5 10
1 2 3 4 5

outputFormat

Output a single integer which is the maximum length of a fortunate segment as per the problem's definition. If no such segment exists, output 0.

For example:

4
## sample
5 10
1 2 3 4 5
4