#K7346. Smallest Subarray with Sum

    ID: 33980 Type: Default 1000ms 256MiB

Smallest Subarray with Sum

Smallest Subarray with Sum

Given an array of \( n \) integers and an integer \( k \), your task is to find the length of the smallest contiguous subarray whose sum is greater than or equal to \( k \). If no such subarray exists, output 0.

A subarray is defined as a sequence of consecutive elements from the array. Efficient algorithms are required to handle large input sizes.

Note: The sum of a subarray \( S[i:j] \) is defined as \( \sum_{t=i}^{j} a_t \), and we require \( \sum_{t=i}^{j} a_t \ge k \).

inputFormat

The input is given in the following format:

n k
a1 a2 ... an

Where:

  • n is the number of elements in the array.
  • k is the target sum.
  • The next line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single integer on a new line: the length of the smallest subarray with a sum greater than or equal to \( k \). If such a subarray does not exist, output 0.

## sample
5 11
1 2 3 4 5
3