#K62392. Minimum Size Subarray Sum
Minimum Size Subarray Sum
Minimum Size Subarray Sum
Given an array a
of n
integers and an integer x
, determine the minimal length of a contiguous subarray such that the sum of its elements is greater than or equal to x
. If no such subarray exists, output -1.
Mathematical formulation:
Find the smallest integer l such that there exists an index i with \[ \sum_{j=i}^{i+l-1} a_j \ge x \] If no valid subarray exists then return -1.
The problem is best approached using a sliding window technique.
inputFormat
The input is given via standard input (stdin) in the following format:
- The first line contains an integer
n
denoting the length of the array. - The second line contains
n
space-separated integers representing the arraya
. - The third line contains an integer
x
.
outputFormat
Output to standard output (stdout) a single integer: the minimal length of a contiguous subarray of a
whose sum is at least x
. If no valid subarray exists, output -1.
5
1 2 3 4 5
11
3