#K79727. Smallest Subarray With Sum
Smallest Subarray With Sum
Smallest Subarray With Sum
You are given an array of positive integers and a target integer x. Your task is to determine the length of the smallest contiguous subarray such that the sum of its elements is greater than or equal to x.
If no such subarray exists, output -1
.
Note: A subarray is a contiguous portion of the array.
The problem can be formally stated using the sliding window technique. The goal is to find the minimum length L such that there exists an interval [i, j] (with i ≤ j) for which:
[ \sum_{k=i}^{j} a_k \geq x ]
If no such L exists, output -1
.
inputFormat
The input is provided via stdin in the following format:
n a1 a2 a3 ... an x
Where:
n
is the number of elements in the array.a1, a2, ... , an
are the array elements.x
is the target sum.
outputFormat
Output the length of the smallest contiguous subarray whose sum is greater than or equal to x
via stdout. If no such subarray exists, output -1
.
5
1 2 3 4 5
11
3