#K9341. Minimum Sections to Collect Bacteria
Minimum Sections to Collect Bacteria
Minimum Sections to Collect Bacteria
You are given an array of n integers representing the bacteria count in each section of a trail. Your task is to determine the minimum number of contiguous sections needed to collect at least m bacteria. If it is not possible to accumulate at least m bacteria by any contiguous subarray, output -1
.
The problem can be formulated as finding the smallest window in the array such that:
[ \sum_{i=l}^{r} a_i \geq m, ]
where l and r are the starting and ending indices of the subarray respectively (1-indexed in the description), and ai is the bacteria count in section i. Use a sliding window algorithm to achieve an efficient solution.
inputFormat
The first line contains two integers n and m where n is the number of sections and m is the minimum bacteria count required.
The second line contains n space-separated integers representing the bacteria counts in each section.
outputFormat
Output a single integer: the minimum number of contiguous sections required to collect at least m bacteria. If it is impossible, output -1
.
5 12
1 2 3 4 5
3