A Sliding Window Problem II
View as PDF is back yet again with another sliding window problem! This time, he imagines a window of size , sliding from the leftmost position in an array to the rightmost. He wants to ensure each window's elements sum to at least
. However, large windows are expensive! He wants to find the minimum window size such that this condition is met.
Formally, he wants to find the minimum value such that all subarrays of size
have a sum of
or greater.
Help solve this problem!
Input Specification
The first line contains two spaced integers
and
, representing the number of elements in the array and the minimum sum required for the sliding window.
The next line contains spaced integers, where the
integer is
.
Output Specification
On a single line, output the minimum window size required such that all windows of size
has a sum of at least
. You may assume that the minimum
will be greater than
and less than or equal to
.
Beware of integer overflow as the sum within a window can exceed the integer limit in some languages such as C++.
Sample Input 1
5 7
2 9 3 2 4
Output for Sample Input 1
3
Explanation of Output for Sample Input 1
The windows of size can be seem below:
Each window has a sum of at least . A window size any smaller, such as a size
, would not.
Sample Input 2
8 5
2 0 3 2 2 0 1 4
Output for Sample Input 2
4
Sample Input 3
6 3
3 3 3 3 3 3
Output for Sample Input 3
1
Comments