#K45142. Minimum Size Subarray Sum

    ID: 27688 Type: Default 1000ms 256MiB

Minimum Size Subarray Sum

Minimum Size Subarray Sum

Given an array of positive integers and a positive integer \( K \), find the minimal length of a contiguous subarray for which the sum is at least \( K \). If no such subarray exists, output 0. This problem requires you to efficiently compute the minimal size of a subarray where the sum \( S \) satisfies \( S \ge K \). You may assume that each element of the array is a positive integer.

Example:

  • For the array [1, 2, 3, 4, 5, 6, 7, 8] and \( K = 15 \), the minimal subarray could be [7, 8] which has a length of 2.
  • For the array [1, 1, 1, 1, 1] and \( K = 11 \), there is no contiguous subarray with a sum at least 11, so the output is 0.

inputFormat

The input is given from the standard input (stdin) and consists of three lines:

  1. An integer \( n \) representing the number of elements in the array.
  2. \( n \) space-separated positive integers representing the elements of the array.
  3. An integer \( K \), the target sum.

outputFormat

Output to the standard output (stdout) a single integer, which is the minimum length of a contiguous subarray of which the sum is at least \( K \). If no such subarray exists, output 0.

## sample
8
1 2 3 4 5 6 7 8
15
2