#K69532. Smallest Period of Rainfall Exceeding Threshold
Smallest Period of Rainfall Exceeding Threshold
Smallest Period of Rainfall Exceeding Threshold
Given an array of integers representing daily rainfall amounts and an integer threshold, your task is to determine the length of the smallest contiguous subarray (period) such that the sum of its elements is at least the given threshold. If no such subarray exists, output -1.
This problem can be solved efficiently using a sliding window technique. As you iterate through the array, maintain a running sum and adjust the window's start position to find the minimum length that satisfies the condition.
The cumulative sum is computed continuously and the window is shrunk from the left when the condition is met.
Formula: Let the contiguous subarray be from index i to j, then we are looking for the minimum (j - i + 1) such that \( \sum_{k=i}^{j} rainfall[k] \geq threshold \).
inputFormat
The first line of input contains two integers, n
and threshold
, where n
is the number of days and threshold
is the target amount of rainfall.
The second line contains n
space-separated integers representing the rainfall amounts for each day.
outputFormat
Output a single integer: the length of the smallest contiguous subarray with a sum that meets or exceeds the threshold. If no such subarray exists, output -1.
## sample6 7
2 3 1 2 4 3
2